Search code examples
ruby-on-railsrubydevise

Rails: Default admin for first registration user on devise and milia


I'm working for a multi-tenant application using Ruby on Rails, Devise, Stripe and milia.

Basic Concept: At first a user create an account by procedural way like using devise sign_up page and create an organization this user has the ability to add multiple members for this organization at this point first user is organization admin and others member/members are the just members like there has no writing permission just reading permission.

At second the first user an send an invitation to a member who will have an organization member, now everything is working like registration, payment and invite to member email and join this member.

I have tried like this

on the user.rb

before_save {self.is_admin = true}

But this saving for all including members.

This is my registration code

class RegistrationsController < Milia::RegistrationsController

    skip_before_action :authenticate_tenant!, :only => [:new, :create, :cancel]

    def create
        # have a working copy of the params in case Tenant callbacks
        # make any changes
        tenant_params = sign_up_params_tenant
        user_params   = sign_up_params_user
        coupon_params = sign_up_params_coupon

        sign_out_session!
        # next two lines prep signup view parameters
        prep_signup_view( tenant_params, user_params, coupon_params )

        # validate recaptcha first unless not enabled
        if !::Milia.use_recaptcha  ||  verify_recaptcha
            Tenant.transaction  do
                @tenant = Tenant.create_new_tenant( tenant_params, user_params, coupon_params)
                if @tenant.errors.empty?   # tenant created
                    if @tenant.plan == 'premium'
                        @payment = Payment.new({email: user_params["email"],
                            token: params[:payment]["token"],
                            tenant: @tenant
                        })

                        flash[:error] = "Please check registration errors" unless @payment.valid?

                        begin 
                            @payment.process_payment
                            @payment.save
                        rescue Exception => e
                            flash[:error] = e.message
                            @tenant.destroy
                            log_action('Payment Failed') 
                            render :new and return
                        end
                    end
                else
                    resource.valid?
                    log_action( "tenant create failed", @tenant )
                    render :new
                end # if .. then .. else no tenant errors

                if flash[:error].blank? || flash[:error].empty?
                    initiate_tenant( @tenant )    # first time stuff for new tenant
                    devise_create( user_params )   # devise resource(user) creation; sets resource
                    if resource.errors.empty?   #  SUCCESS!
                        log_action( "signup user/tenant success", resource )
                        # do any needed tenant initial setup
                        Tenant.tenant_signup(resource, @tenant, coupon_params)
                    else  # user creation failed; force tenant rollback
                        log_action( "signup user create failed", resource )
                        raise ActiveRecord::Rollback   # force the tenant transaction to be rolled back
                    end  # if..then..else for valid user creation
                else
                    resource.valid?
                    log_action('Payment proccesing fails', @tenant)
                    render :new and return
                end # if. . then .. else no tenant errors
            end  #  wrap tenant/user creation in a transaction
        else
            flash[:error] = "Recaptcha codes didn't match; please try again"
           # all validation errors are passed when the sign_up form is re-rendered
            resource.valid?
            @tenant.valid?
            log_action( "recaptcha failed", resource )
            render :new
        end
    end   # def create
end

My question is: How to create is_admin: true for who create organization?

Thanks!


Solution

  • If I understood your concept like you need to assign is_admin: true for who can register using this registration controller, Right? If yes then it's very easy update this user_params = sign_up_params_user line of code

    Try the following

    user_params   = sign_up_params_user.merge({ is_admin: true })
    

    Now assign is_admin: true for only who can create an account with an organization.

    Now if you block to special permission for normal members then create a method to user.rb file like

    def is_admin?
      is_admin
    end
    

    Then for permission

    if current_user.is_admin?
       #=> Permission for admin
    else
       #=> Permission denied for normal members
    end
    

    Hope it helps