Comments (4)
@vlad Thanks. The reason is there may be abilities that are not optional and are common to all users such as;
cannot :destroy, User, :id => user.id
I treat roles as additions to the common abilities. So even if you don't have any roles defined you can still define abilities for a user.
class User < ActiveRecord::Base
acts_as_user
end
Will load app/abilities/users.rb. The roles augment the common abilities with further abilities that role can perform. If the ability is optional it should be defined in a role, if it is common to all users it goes in the common (user) abilities.
Given your example, I would ask yourself what your :user role does that is not common to all users and separate them out into a role that describes the difference.
from canard.
I think we are approaching this from different angles. I don't regard the User abilities as a role. I approach it like this;
u = User.create(....)
u.roles = [:admin]
u.save
u.is_a? User # true
u.has_role? :admin # true
u.has_role? :manager # false
u.has_role? :user # false
In fact I almost never use has_role? for anything except perhaps as a catchall for an admin area in the controller
class Admin::BaseController < ApplicationController
before_filter :authenticate_admin
private
def authenticate_admin
raise CanCan::AccessDenied.new("You need to be an administrator to
access the admin area.") unless current_user.has_role?(:admin)
end
end
Otherwise I leave everything to CanCan and the abilities because it means I don't have to refactor anything but the abilities if I choose to change what a role can access. This makes accessing models cleaner with CanCan's accessible_by scope, and is why I inject an ability method with acts_as_user.
I have some applications where the authentication is in a separate model and there is no User class but an Account model with a polymorphic user association to one of several classes e.g. Vendor, Customer. Both Vendor and Customer 'act_as_user' but each has multiple roles and a Vendor has very different common abilities to a Customer.
So in short, from my perspective I don't regard the User instance in the example above as having a role of :user, it is just a User instance.
If you do want has_role? to return true as per your example you could override the method in your User class;
def has_role?(role)
String(role).classify == self.class.name || super
end
from canard.
Closing as it's not a bug but a design choice to separate the base class abilities from the Roles.
from canard.
Thanks for the responses James. I was mostly suggesting it might be
helpful to mention/remind this design decision in the README; you should
likely not change the way role_model works since this is a wrapper around
it. You're absolutely right that this makes sense.
On Tue, Aug 21, 2012 at 10:00 AM, James McCarthy
[email protected]:
Closing as it's not a bug but a design choice to separate the base class
abilities from the Roles.—
Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-7908025.
from canard.
Related Issues (15)
- test_unit[not found] HOT 1
- Heroku, devise, and canard fails on rake assets:precompile HOT 2
- Support for rolify?
- Non-inherited Roles HOT 2
- Multiple Abilities? HOT 4
- cancancan support HOT 3
- Bundler won't install canard unless Rails is already installed HOT 1
- Allow one role to inherit abilities of other roles HOT 2
- Get All Available Roles
- How to stub the ability in the step_definitions HOT 3
- Instalation HOT 3
- Updating Canard to pre rolls back version of CanCanCan HOT 1
- How to use cancard with separate context? HOT 3
- User abilities don't include guest abilities HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from canard.