jekuno / milia

Easy multi-tenanting for Rails5 (or Rails4) + Devise
MIT License
341 stars 72 forks source link

[register user/org] tenant create failed - Tenant must exist #87

Open sonq opened 5 years ago

sonq commented 5 years ago

Hello all!

I am trying to use milia with devise for my project however i am facing some issues.

When i try to create a user it gives me this error: × 1 error prohibited this user from being saved: Tenant must exist

And this is the log

Started POST "/users" for 127.0.0.1 at 2018-12-18 15:36:23 +0300 Processing by Milia::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"MfVgYtGpIITDL5qQBLTCCbgQ23CRc6ChlgNy64lXy9kwYDQcvmWIWHbQsZnkQdMFcolHhMvZAKoCevdp3zfy5w==", "user"=>{"email"=>"sk@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "tenant"=>{"name"=>"TTOPCO", "plan"=>"free"}, "commit"=>"Sign up"} (0.1ms) BEGIN Tenant Exists (0.3ms) SELECT 1 AS one FROM "tenants" WHERE "tenants"."name" = $1 LIMIT $2 [["name", "TTOPCO"], ["LIMIT", 1]] User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "sk@test.com"], ["LIMIT", 1]] MILIA >>>>> [register user/org] tenant create failed - Tenant must exist Rendering devise/registrations/new.html.erb within layouts/application Rendered devise/shared/_links.html.erb (1.0ms) Rendered devise/registrations/new.html.erb within layouts/application (6.0ms) (0.2ms) COMMIT Completed 200 OK in 193ms (Views: 57.4ms | ActiveRecord: 1.0ms)

This is my User model

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  acts_as_universal_and_determines_account
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :validatable
end

and this is my tenant model

> class Tenant < ApplicationRecord

  acts_as_universal_and_determines_tenant
  has_many :members, dependent: :destroy

  validates_uniqueness_of :name
  validates_presence_of :name

    def self.create_new_tenant(tenant_params, user_params, coupon_params)

      tenant = Tenant.new(tenant_params)

      if new_signups_not_permitted?(coupon_params)

        raise ::Milia::Control::MaxTenantExceeded, "Sorry, new accounts not permitted at this time" 

      else 
        tenant.save    # create the tenant
      end
      return tenant
    end

  # ------------------------------------------------------------------------
  # new_signups_not_permitted? -- returns true if no further signups allowed
  # args: params from user input; might contain a special 'coupon' code
  #       used to determine whether or not to allow another signup
  # ------------------------------------------------------------------------
  def self.new_signups_not_permitted?(params)
    return false
  end

  # ------------------------------------------------------------------------
  # tenant_signup -- setup a new tenant in the system
  # CALLBACK from devise RegistrationsController (milia override)
  # AFTER user creation and current_tenant established
  # args:
  #   user  -- new user  obj
  #   tenant -- new tenant obj
  #   other  -- any other parameter string from initial request
  # ------------------------------------------------------------------------
    def self.tenant_signup(user, tenant, other = nil)
      #  StartupJob.queue_startup( tenant, user, other )
      # any special seeding required for a new organizational tenant
      #
      Member.create_org_admin(user)
      #
    end

end

I am using rails 5.1. I tried to comment out the config.load_defaults 5.1 in my application.rb file however i got this error

Started POST "/users" for 127.0.0.1 at 2018-12-18 15:55:38 +0300 Processing by Milia::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"YPEO6XBrOYWrWEutVKH71VUWc/cj0z8r1lnk5g0+IzFhZFqXH6eRWR6nYKS0VOrZn4/vA3l5nyBCIGFkW14aDw==", "user"=>{"email"=>"sk@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "tenant"=>{"name"=>"test", "plan"=>"free"}, "commit"=>"Sign up"} (0.2ms) BEGIN Tenant Exists (1.0ms) SELECT 1 AS one FROM "tenants" WHERE "tenants"."name" = $1 LIMIT $2 [["name", "test"], ["LIMIT", 1]] SQL (0.6ms) INSERT INTO "tenants" ("name", "created_at", "updated_at", "plan") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "test"], ["created_at", "2018-12-18 12:55:38.641784"], ["updated_at", "2018-12-18 12:55:38.641784"], ["plan", "free"]] MILIA >>>>> [change tenant] new: 32 old: % User Exists (0.9ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "sk@test.com"], ["LIMIT", 1]] SQL (0.5ms) INSERT INTO "users" ("email", "encrypted_password", "confirmation_token", "confirmation_sent_at", "skip_confirm_change_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["email", "sk@test.com"], ["encrypted_password", "$2a$11$l4ZxbFsxh2cJmtUMXS4qtOr6yu6pyvM/cFEwCZPsOgjbcJIOkzzIK"], ["confirmation_token", "b6JnpycEemiXSsy1qv7G"], ["confirmation_sent_at", "2018-12-18 12:55:38.776925"], ["skip_confirm_change_password", "t"], ["created_at", "2018-12-18 12:55:38.776678"], ["updated_at", "2018-12-18 12:55:38.776678"]] Tenant Load (0.5ms) SELECT "tenants".* FROM "tenants" WHERE (tenants.tenant_id IS NULL) AND "tenants"."id" = $1 LIMIT $2 [["id", 32], ["LIMIT", 1]] User Exists (1.0ms) SELECT 1 AS one FROM "users" INNER JOIN "tenants_users" ON "users"."id" = "tenants_users"."user_id" WHERE (users.tenant_id IS NULL) AND "tenants_users"."tenant_id" = $1 AND "users"."id" = $2 LIMIT $3 [["tenant_id", 32], ["id", 29], ["LIMIT", 1]] SQL (0.3ms) INSERT INTO "tenants_users" ("tenant_id", "user_id") VALUES ($1, $2) [["tenant_id", 32], ["user_id", 29]] MILIA >>>>> [register user/org] devise: signup user success - Redirected to http://localhost:3000/ MILIA >>>>> [register user/org] signup user/tenant success - (0.4ms) ROLLBACK Completed 500 Internal Server Error in 385ms (ActiveRecord: 12.9ms)

NoMethodError (undefined method `create_member' for # Did you mean? created_at):

app/models/member.rb:13:in create_new_member' app/models/member.rb:17:increate_org_admin' app/models/tenant.rb:45:in `tenant_signup'

Thanks in advance! Soner

Ronald-Mundell commented 5 years ago

You miss one line in your User model

has_one :member, :dependent => :destroy

emrahyildirim commented 4 years ago

This not working. I'm getting the same warning. Don't you think it's time to clean things up?

rohit22g1990 commented 4 years ago

Any updates on this? I am also facing the same issue.