First stage of refactoring of the ability model and spec
This commit is contained in:
parent
3bd82039ad
commit
ac1d9fd329
5 changed files with 214 additions and 118 deletions
|
|
@ -1,21 +1,8 @@
|
|||
class Ability
|
||||
include CanCan::Ability
|
||||
|
||||
# Initializes the ability class
|
||||
def initialize(user)
|
||||
# The first argument to `can` is the action you are giving the user permission to do.
|
||||
# If you pass :manage it will apply to every action. Other common actions here are
|
||||
# :read, :create, :update and :destroy.
|
||||
#
|
||||
# The second argument is the resource the user can perform the action on. If you pass
|
||||
# :all it will apply to every resource. Otherwise pass a Ruby class of the resource.
|
||||
#
|
||||
# The third argument is an optional hash of conditions to further filter the objects.
|
||||
# For example, here the user can only update published articles.
|
||||
#
|
||||
# can :update, Article, :published => true
|
||||
#
|
||||
# See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities
|
||||
|
||||
# Order Abilities
|
||||
# (Check https://github.com/CanCanCommunity/cancancan/wiki/Ability-Precedence)
|
||||
# Check roles of user, using rolify. Role name is *case sensitive*
|
||||
|
|
@ -27,21 +14,75 @@ class Ability
|
|||
# The following is wrong because a user will only have 'cfp' role for a specific conference
|
||||
# user.is_cfp? # This is always false
|
||||
|
||||
user ||= User.new # guest user (not logged in)
|
||||
user ||= User.new
|
||||
|
||||
# This is what sets up the different abilities
|
||||
if user.new_record?
|
||||
guest
|
||||
not_signed_in
|
||||
else
|
||||
# This maps the actionables name of the role to its name in the DB.
|
||||
roles = Role::ACTIONABLES.map {|i| i.parameterize.underscore}
|
||||
if (user.roles.pluck(:name) & roles).empty? && !user.is_admin # User has no roles
|
||||
# Checks if the user does not have any role and is not an admin
|
||||
if (user.roles.pluck(:name) & roles).empty? && !user.is_admin
|
||||
signed_in(user)
|
||||
else
|
||||
user_with_roles(user)
|
||||
signed_in_with_roles(user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def user_with_roles(user)
|
||||
# Abilities for not signed in users (guests)
|
||||
def not_signed_in
|
||||
can [:index], Conference
|
||||
can [:show], Conference do |conference|
|
||||
conference.splashpage && conference.splashpage.public == true
|
||||
end
|
||||
# Can view the schedule
|
||||
can [:schedule], Conference do |conference|
|
||||
conference.call_for_paper && conference.call_for_paper.schedule_public
|
||||
end
|
||||
|
||||
can :show, Event do |event|
|
||||
event.state == 'confirmed'
|
||||
end
|
||||
|
||||
# can view Commercials of confirmed Events
|
||||
can :show, Commercial, commercialable_type: 'Event', commercialable_id: Event.where(state: 'confirmed').pluck(:id)
|
||||
can :show, User
|
||||
can [:show, :create], Registration do |registration|
|
||||
registration.new_record?
|
||||
end
|
||||
end
|
||||
|
||||
# Abilities for signed in users
|
||||
def signed_in(user)
|
||||
# Abilities from not_signed_in user are also inherited
|
||||
not_signed_in
|
||||
|
||||
can :manage, User, id: user.id
|
||||
|
||||
can :manage, Registration, user_id: user.id
|
||||
|
||||
can :index, Ticket
|
||||
can :manage, TicketPurchase, user_id: user.id
|
||||
|
||||
can [:create, :destroy], Subscription, user_id: user.id
|
||||
|
||||
can :manage, Event do |event|
|
||||
event.users.include?(user)
|
||||
end
|
||||
# can create an event until the last day of a conference
|
||||
can :create, Event, conference_id: Conference.where('end_date >= ?', Date.today).pluck(:id)
|
||||
|
||||
# can manage the commercials of their own events
|
||||
can :manage, Commercial, commercialable_type: 'Event', commercialable_id: user.events.pluck(:id)
|
||||
end
|
||||
|
||||
# Abilities for signed in users with roles
|
||||
def signed_in_with_roles(user)
|
||||
# Abilities from not_signed_in and signed_in are also inherited
|
||||
signed_in(user)
|
||||
|
||||
conf_ids_for_organizer = []
|
||||
conf_ids_for_cfp = []
|
||||
conf_ids_for_info_desk = []
|
||||
|
|
@ -59,7 +100,6 @@ class Ability
|
|||
conf_ids_for_volunteer_coordinator =
|
||||
Conference.with_role(:volunteer_coordinator, user).pluck(:id) if user.has_role? :volunteer_coordinator, :any
|
||||
|
||||
signed_in(user) # Inherit abilities from signed user
|
||||
# User with role
|
||||
can [:new, :create], Conference if user.is_admin || (user.has_role? :organizer, :any)
|
||||
can [:index, :show, :gallery_photos], Conference
|
||||
|
|
@ -101,52 +141,4 @@ class Ability
|
|||
can :index, Venue, conference_id: conf_ids_for_organizer + conf_ids_for_cfp
|
||||
can :manage, :all if user.is_admin
|
||||
end
|
||||
|
||||
# Abilities for everyone, even guests (not logged in users)
|
||||
def guest
|
||||
# can view conferences
|
||||
can [:index], Conference
|
||||
can [:show], Conference do |conference|
|
||||
conference.splashpage && conference.splashpage.public == true
|
||||
end
|
||||
can [:schedule], Conference do |conference|
|
||||
conference.call_for_paper && conference.call_for_paper.schedule_public
|
||||
end
|
||||
|
||||
# can view confirmed Events
|
||||
can :show, Event do |event|
|
||||
event.state == 'confirmed'
|
||||
end
|
||||
# can view Commercials of confirmed Events
|
||||
can :show, Commercial, commercialable_type: 'Event', commercialable_id: Event.where(state: 'confirmed').pluck(:id)
|
||||
# can view others
|
||||
can :show, User
|
||||
# can register
|
||||
can [:read, :create], Registration do |registration|
|
||||
registration.new_record?
|
||||
end
|
||||
end
|
||||
|
||||
def signed_in(user)
|
||||
guest # Inherits abilities of guest
|
||||
|
||||
# subscribe, unsubscribe to a Conference
|
||||
can [:create, :destroy], Subscription, user_id: user.id
|
||||
|
||||
# can manage their own Registration
|
||||
can :manage, Registration, user_id: user.id
|
||||
can :index, Ticket
|
||||
can :manage, TicketPurchase, user_id: user.id
|
||||
|
||||
# can manage their own User
|
||||
can :manage, User, id: user.id
|
||||
cannot :index, User
|
||||
|
||||
# can manage their own Event
|
||||
can :manage, Event, id: user.events.pluck(:id)
|
||||
# can submit Events for conferences that are not over yet
|
||||
can :create, Event, conference_id: Conference.where('end_date >= ?', Date.today).pluck(:id)
|
||||
# can manage their own commercials
|
||||
can :manage, Commercial, commercialable_type: 'Event', commercialable_id: user.events.pluck(:id)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue