Rails: adding an administrator role using an application that can see all users

I need to create an administrator role using the program for my application. I created basic authentication using an application. I have a model of user models in my application, but now I need an administrator who can show edits and destroy all users. I tried to follow the tutorials, but none of them helped. I am using rails 3.0.10 and ruby ​​1.9.2-p290.

+3
source share
2 answers

First you define role.rb by creating migratioin

rails g model role name:string 

then in role.rb

  class Role has_one:user end 

And in the user model

  class user belongs_to :role end 

Insert two roles in DB

  1.admin 2.user 

Then check it out

  if user.role.name == "admin" # can do all your logic else your logic end 

Make sure insert role_id: integer in user model

Try it .......

+3
source

I have a similar requirement, as you do, and also do not want any user to be able to register. All that will be taken care of by the administrator. Her is what I did.

I added another development model called admin

 rails generate devise MODEL 

Disable "Registered" for the user model so that the user cannot at his discretion

user.rb

 class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :registerable, :timeoutable and :omniauthable devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :role, :admin # attr_accessible :title, :body end 

Enable CRUD for the user using the sample from here: https://gist.github.com/1056194

Finally, protect the user controller in this way.

users_controller.rb

 # Add this before_filter :authenticate_admin! 

Hope this helps.

+1
source

Source: https://habr.com/ru/post/913962/


All Articles