2013-01-07 09:04:09 +01:00
|
|
|
class Ability
|
|
|
|
|
include CanCan::Ability
|
|
|
|
|
|
2014-07-23 16:24:05 +02:00
|
|
|
def initialize(user)
|
2014-08-12 11:51:59 +03:00
|
|
|
# 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*
|
|
|
|
|
# user.is_organizer? or user.has_role? :organizer
|
|
|
|
|
# user.is_cfp_of? Conference or user.has_role? :cfp, Conference
|
|
|
|
|
# user.is_info_desk_of? Conference
|
|
|
|
|
# user.is_volunteer_coordinator_of? Conference
|
|
|
|
|
# user.is_attendee_of? Conference
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
|
|
if user.new_record?
|
2014-08-12 20:23:51 +03:00
|
|
|
guest
|
2014-07-23 16:24:05 +02:00
|
|
|
else
|
2014-08-12 11:51:59 +03:00
|
|
|
roles = Role::ACTIONABLES.map {|i| i.parameterize.underscore}
|
|
|
|
|
if (user.roles.pluck(:name) & roles).empty? && !user.is_admin # User has no roles
|
|
|
|
|
signed_in(user)
|
|
|
|
|
else
|
|
|
|
|
user_with_roles(user)
|
2014-07-23 16:24:05 +02:00
|
|
|
end
|
|
|
|
|
end
|
2013-01-07 09:04:09 +01:00
|
|
|
end
|
2014-08-12 11:51:59 +03:00
|
|
|
|
|
|
|
|
def user_with_roles(user)
|
|
|
|
|
conf_ids_for_organizer = []
|
|
|
|
|
conf_ids_for_cfp = []
|
|
|
|
|
conf_ids_for_info_desk = []
|
|
|
|
|
conf_ids_for_volunteer_coordinator = []
|
|
|
|
|
|
|
|
|
|
# Ids of all the conferences for which the user has an 'organizer' role
|
|
|
|
|
conf_ids_for_organizer =
|
|
|
|
|
Conference.with_role(:organizer, user).pluck(:id) if user.has_role? :organizer, :any
|
|
|
|
|
conf_ids_for_cfp =
|
|
|
|
|
Conference.with_role(:cfp, user).pluck(:id) if user.has_role? :cfp, :any
|
|
|
|
|
# Ids of all the conferences for which the user has an 'info_desk' role
|
|
|
|
|
conf_ids_for_info_desk =
|
|
|
|
|
Conference.with_role(:info_desk, user).pluck(:id) if user.has_role? :info_desk, :any
|
|
|
|
|
# Ids of all the conferences for which the user has a 'volunteer_coordinator' role
|
|
|
|
|
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
|
|
|
|
|
can :manage, Conference, id: conf_ids_for_organizer
|
|
|
|
|
# can :manage, Conference do |conference|
|
|
|
|
|
# conference.id = conf_ids_for_organizer
|
|
|
|
|
# end
|
|
|
|
|
can :manage, Registration, conference_id: conf_ids_for_organizer + conf_ids_for_info_desk
|
|
|
|
|
can :manage, Question, conference_id: conf_ids_for_organizer + conf_ids_for_info_desk
|
2014-08-12 16:33:01 +03:00
|
|
|
cannot [:edit, :update, :destroy], Question, global: true
|
2014-08-12 11:51:59 +03:00
|
|
|
can :manage, Vposition, conference_id: conf_ids_for_organizer + conf_ids_for_volunteer_coordinator
|
|
|
|
|
can :manage, Vday, conference_id: conf_ids_for_organizer + conf_ids_for_volunteer_coordinator
|
|
|
|
|
# The ability to manage an Event means that:
|
|
|
|
|
# the user can also edit the schedule and that
|
|
|
|
|
# the user can also vote
|
|
|
|
|
can :manage, Event, conference_id: conf_ids_for_organizer + conf_ids_for_cfp
|
|
|
|
|
can :create, Event
|
|
|
|
|
can :manage, EventType, conference_id: conf_ids_for_organizer + conf_ids_for_cfp
|
|
|
|
|
can :manage, Track, conference_id: conf_ids_for_organizer + conf_ids_for_cfp
|
|
|
|
|
can :manage, DifficultyLevel, conference_id: conf_ids_for_organizer + conf_ids_for_cfp
|
|
|
|
|
can :manage, EmailSettings, conference_id: conf_ids_for_organizer + conf_ids_for_cfp
|
|
|
|
|
can :manage, Campaign, conference_id: conf_ids_for_organizer
|
2014-11-20 14:57:03 +01:00
|
|
|
can :manage, Lodging, conference_id: conf_ids_for_organizer
|
2014-08-12 11:51:59 +03:00
|
|
|
can :manage, Room, conference_id: conf_ids_for_organizer + conf_ids_for_cfp
|
|
|
|
|
can :manage, Sponsor, conference_id: conf_ids_for_organizer
|
|
|
|
|
can :manage, SponsorshipLevel, conference_id: conf_ids_for_organizer
|
2014-08-28 15:21:05 +02:00
|
|
|
can :manage, Ticket, conference_id: conf_ids_for_organizer
|
2014-08-12 11:51:59 +03:00
|
|
|
can :manage, Target, conference_id: conf_ids_for_organizer
|
|
|
|
|
can :index, Commercial, commercialable_type: 'Conference'
|
2014-08-13 14:37:15 +03:00
|
|
|
can :manage, Commercial, commercialable_type: 'Conference', commercialable_id: conf_ids_for_organizer
|
2014-08-12 11:51:59 +03:00
|
|
|
# Manage commercials for events that belong to a conference of which user is organizer
|
|
|
|
|
can :manage, Commercial, commercialable_type: 'Event', commercialable_id: Event.where(conference_id: conf_ids_for_organizer + conf_ids_for_cfp).pluck(:id)
|
|
|
|
|
can :manage, Contact, conference_id: conf_ids_for_organizer
|
|
|
|
|
can :manage, Campaign, conference_id: conf_ids_for_organizer
|
2014-08-18 14:33:02 +02:00
|
|
|
can :manage, RegistrationPeriod, conference_id: conf_ids_for_organizer
|
2014-08-30 12:08:24 +02:00
|
|
|
can :manage, Splashpage, conference_id: conf_ids_for_organizer
|
2014-11-25 10:47:18 +01:00
|
|
|
can :manage, CallForPaper, conference_id: conf_ids_for_organizer + conf_ids_for_cfp
|
|
|
|
|
can :manage, Venue, conference_id: conf_ids_for_organizer
|
|
|
|
|
can :index, Venue, conference_id: conf_ids_for_organizer + conf_ids_for_cfp
|
2014-11-27 15:15:34 +01:00
|
|
|
can :manage, :all if user.is_admin
|
2014-08-12 11:51:59 +03:00
|
|
|
end
|
|
|
|
|
|
2014-11-27 15:15:34 +01:00
|
|
|
# Abilities for everyone, even guests (not logged in users)
|
2014-08-12 20:23:51 +03:00
|
|
|
def guest
|
2014-11-27 15:15:34 +01:00
|
|
|
# can view conferences
|
2014-12-05 16:04:34 +01:00
|
|
|
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
|
|
|
|
|
|
2014-11-27 15:15:34 +01:00
|
|
|
# can view confirmed Events
|
2014-08-12 11:51:59 +03:00
|
|
|
can :show, Event do |event|
|
|
|
|
|
event.state == 'confirmed'
|
|
|
|
|
end
|
2014-11-27 15:15:34 +01:00
|
|
|
# can view Commercials of confirmed Events
|
|
|
|
|
can :show, Commercial, commercialable_type: 'Event', commercialable_id: Event.where(state: 'confirmed').pluck(:id)
|
|
|
|
|
# can view others
|
2014-11-04 17:42:47 +01:00
|
|
|
can :show, User
|
2014-08-12 11:51:59 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def signed_in(user)
|
2014-08-12 20:23:51 +03:00
|
|
|
guest # Inherits abilities of guest
|
2014-08-12 11:51:59 +03:00
|
|
|
|
2014-11-27 15:15:34 +01:00
|
|
|
# subscribe, unsubscribe to a Conference
|
2014-10-31 22:45:15 +02:00
|
|
|
can [:create, :destroy], Subscription, user_id: user.id
|
|
|
|
|
|
2014-11-27 15:15:34 +01:00
|
|
|
# can manage their own Registration
|
2014-08-12 11:51:59 +03:00
|
|
|
can :manage, Registration, user_id: user.id
|
2014-12-18 11:42:42 +01:00
|
|
|
can :index, Ticket
|
|
|
|
|
can :manage, TicketPurchase, user_id: user.id
|
2014-08-12 11:51:59 +03:00
|
|
|
|
2014-11-27 15:15:34 +01:00
|
|
|
# can manage their own User
|
2014-11-04 17:42:47 +01:00
|
|
|
can :manage, User, id: user.id
|
2014-12-11 13:15:37 +02:00
|
|
|
cannot :index, User
|
2014-11-04 17:42:47 +01:00
|
|
|
|
2014-11-27 15:15:34 +01:00
|
|
|
# can manage their own Event
|
2014-08-12 11:51:59 +03:00
|
|
|
can :manage, Event, id: user.events.pluck(:id)
|
2014-11-27 15:15:34 +01:00
|
|
|
# can submit Events for conferences that are not over yet
|
2014-08-12 11:51:59 +03:00
|
|
|
can :create, Event, conference_id: Conference.where('end_date >= ?', Date.today).pluck(:id)
|
2014-11-27 15:15:34 +01:00
|
|
|
# can manage their own commercials
|
2014-08-12 11:51:59 +03:00
|
|
|
can :manage, Commercial, commercialable_type: 'Event', commercialable_id: user.events.pluck(:id)
|
|
|
|
|
end
|
2013-01-07 09:04:09 +01:00
|
|
|
end
|