cancan

Access CanCan's `can?` method from a model

我是研究僧i 提交于 2019-11-27 17:58:24
You can get the current_user 's permissions from a view or controller using can? in this fashion: <% if can? :update, @article %> <%= link_to "Edit", edit_article_path(@article) %> <% end %> How can I access this functionality from a model using this syntax: user.can?(:update, @article) There's a wiki entry at github for this: http://wiki.github.com/ryanb/cancan/ability-for-other-users You need to change your User model like this: class User < ActiveRecord::Base def ability @ability ||= Ability.new(self) end delegate :can?, :cannot?, :to => :ability end Then you can check abilities like this:

How to do integration testing with RSpec and Devise/CanCan?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 17:10:38
If I have a Devise model User, of which only those users with role :admin are allowed to view a certain url, how can I write an RSpec integration test to check that the status returns 200 for that url? def login(user) post user_session_path, :email => user.email, :password => 'password' end This was pseudo-suggested in the answer to this question: Stubbing authentication in request spec , but I can't for the life of me get it to work with devise. CanCan is receiving a nil User when checking Ability, which doesn't have the correct permissions, naturally. There's no access to the controller in

How can I redirect a user's home (root) path based on their role using Devise?

余生长醉 提交于 2019-11-27 11:15:11
I'm working on a project management app, and in the app, I have project_managers and clients . I'm using Devise and CanCan for authentication/authorization. At what point after login should I be redirecting the user to their own specific controller/layout/views? Is there a way to check for current_user.role in routes.rb and set the root (or redirect) based on whether or not they're a project manager or a client? Is this a change I can make in Devise somewhere? Thanks in advance for any help! --Mark vonconrad Your routes.rb file won't have any idea what role the user has, so you won't be able

Access CanCan's `can?` method from a model

瘦欲@ 提交于 2019-11-26 19:14:20
问题 You can get the current_user 's permissions from a view or controller using can? in this fashion: <% if can? :update, @article %> <%= link_to "Edit", edit_article_path(@article) %> <% end %> How can I access this functionality from a model using this syntax: user.can?(:update, @article) 回答1: There's a wiki entry at github for this: https://github.com/ryanb/cancan/wiki/ability-for-other-users You need to change your User model like this: class User < ActiveRecord::Base def ability @ability ||=

How to do integration testing with RSpec and Devise/CanCan?

…衆ロ難τιáo~ 提交于 2019-11-26 18:56:14
问题 If I have a Devise model User, of which only those users with role :admin are allowed to view a certain url, how can I write an RSpec integration test to check that the status returns 200 for that url? def login(user) post user_session_path, :email => user.email, :password => 'password' end This was pseudo-suggested in the answer to this question: Stubbing authentication in request spec, but I can't for the life of me get it to work with devise. CanCan is receiving a nil User when checking

How can I redirect a user's home (root) path based on their role using Devise?

白昼怎懂夜的黑 提交于 2019-11-26 17:59:38
问题 I'm working on a project management app, and in the app, I have project_managers and clients . I'm using Devise and CanCan for authentication/authorization. At what point after login should I be redirecting the user to their own specific controller/layout/views? Is there a way to check for current_user.role in routes.rb and set the root (or redirect) based on whether or not they're a project manager or a client? Is this a change I can make in Devise somewhere? Thanks in advance for any help!