For the Rolify gem in Rails in order to add a role to a user you can do:
user = User.find(1)
user.add_role :admin
But I have a large database of already existing users with no roles. How do I add a role to multiple users with a single command?
I tried the snippet below but it errored:
users = User.where(email:['email1','email2'])
users.addrole :admin
Does anyone know how to do this? Or do I need to create a script that cycles through the users automatically and assigns a role one by one?
Rolify is mostly just a quick and dirty role system, and it currently doesn't provide this feature.
You'll have to write a bulk insert/upsert query of your own into the database tables that Rolify persists data, e.g. (roles and users_roles for a users table).
Related
I have a goal to have a User model and a Vendor model. Think as if User is a company, and Vendor is employees where the company can create multiple employee accounts to associate to the company.
I have set up Sorcery to have User model and Vendor model but when signing in is where my issues are.
At the end of the sorcery file, there is this:
config.user_class = "User"
I have tried:
config.user_class = "User" || "Vendor"
When signing in, it will only search through the model that is first.
this:
config.user_class = "User"
config.user_class = "Vendor"
(in both orders, vice versa)
Whichever is last, that model works. It will search only through the Vendor model if the Vendor model is last.
I looked into STI, but most of what I have seen is creating associated models that inherit from the one model. Not much on multiple models for authentication for each, or any. Is this a route to go into?
I need both User and Vendor to be able to sign in as separate accounts.
I have, in the past, used Devise to create multiple models with ease but Sorcery seems to be giving issues?
Is there a way to set up Sorcery to allow multiple models?
Sorcery does allow STI though the user.subclasses_inherit_config setting in config/initializers/sorcery.rb. You can set it to true then inherit your Vendor class from User.
I've never used the Gem but I would look at just adding a role to your user class and use authorization.
I'm developing and application with ruby on rails . i have the following models : user, role, option, permission. Depending on the Role of an User i want the menu of the application to display certain options. So an USER has one ROLE, one ROLE has one or many OPTIONS (availables in the menu),one OPTION can be assigned to many Roles . that's why i created a join table called PERMISSION which has rol_id , option_id and status.
SO, in the app, i want to be able to create a new role and check from a list the options this Role can have. But i don't know how to do a form that let me handle all this information and assign the role_id and the option_id the join table needs.
"Best" Solution:
Code it yourself, you should definitely become more familiar with ActiveRecord and be comfortable utilizing relationship tables and the roles one may have, along with writing helper functions like "is_an_admin?" or "is_a_moderator?"
You should also be comfortable with the routes and controllers for adding new users and checking permissions for can POST / UPDATE / PATCH / DELETE entries on your roles database.
Some db like "roles" where you store a user_id and role_level (1 is super_admin, 2 admin, or 3 moderator etc etc) and a user "has_one :role" association?
So my honest recommendation would be to learn it properly, here's some resources:
CULTTT Implementing Roles and Permissions
Association Basics
Easiest Solution
Use a gem, some options:
rolify (https://github.com/RolifyCommunity/rolify)
CanCanCan (https://github.com/CanCanCommunity/cancancan)
Pundit (https://github.com/elabs/pundit)
I have installed Devise and created Users and also Admins using Option 1 from this tutorial https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role
Now I need a little help. When adding the Admins it creates a different table, and the potential arises that the admins and regular users could have the same ID's.
So how exactly do I got about grabbing information from users and admins? Say for example I want to display all users? Do I have to traverse the users table and then the admins table?
Or if I am displaying a post. How will I know which table to look for to get the user or admin info?
I know I can just add a role column to the users table but I wanted to avoid this.
I hope this makes sense haha
I highly recommend using the Single Table Inheritance (STI). You could do the STI by adding a column named type with a string datatype in the users table and create an admin model as well as a normal_user model both models will inherit from the user model of the devise gem.
class NormalUser < User
end
class Admin < User
end
The type column is a reserved word which will hold a value either NormalUser or Admin according to the user type you created.
To create an admin use Admin.create(...) and to create a normal user use NormalUser.create(...) where the dots are the attributes of the Admin and the NormalUser
I have authlogic working fine but now have the need to let administrator users come in and have different access.
So I created a migration adding administrator column to the Users table as string. However, I just can not seem to get the value of it out!!
see below my index action:
print "\n administrator" + User.find(current_user.id).administrator.to_s
the above line never prints anything when I know that this user HAS administrator string set to true in the db.
Below is the User model
class User < ActiveRecord::Base
acts_as_authentic {|config|
config.validates_uniqueness_of_email_field_options :scope => :id
}
belongs_to :another_class
end
what am I doing wrong here? All I want to do is get the administrator column value out. In the logs I can see the select users.* from users where id = 2 query being run!
is there another best way to manage admin roles with authlogic??
You're likely better off using cancan for managing roles and permissions. It can be used in combination with authlogic.
If you are going to just add a flag "administrator" field to the user record, it should work as long as your rules are simple, but would be better as a boolean rather than a string.
You say you have an "administrator" column of strings in your database and you're looking for a value of true. Maybe you wanted a Boolean column?
I am using Test to test my application. I have some fixtures: users.yml, roles.yml, roles_users.yml. The users and roles are loaded but not the many to many table roles_users so the users are not related to any role. With Rails console in development everything is ok, in test any user is not connected to any role. I can see the users and roles in the test database but roles_users is empty
Do I have to specify somewhere how to load this fixture?
If you're using Rails >=2.2 and a standard HABTM association between users and roles, you shouldn't need the roles_users.yml file. Instead, add a roles line for each user in users.yml:
sally:
roles: admin, editor
...
fred:
roles: basic
...
The values are the names of your role fixtures. I'm not completely sure this will solve your problem, but it's cleaner at the very least.