2012-09-02 11:37:34 +00:00
|
|
|
class Ability
|
|
|
|
include CanCan::Ability
|
|
|
|
|
|
|
|
def initialize(user)
|
2013-02-01 10:37:30 +00:00
|
|
|
# Anonymous can read mac
|
|
|
|
can :read, Mac
|
|
|
|
|
2012-09-16 01:41:55 +00:00
|
|
|
if !user.nil?
|
2013-01-26 01:21:42 +00:00
|
|
|
|
2013-01-25 13:01:02 +00:00
|
|
|
# By default, users can only see their own stuff
|
|
|
|
can :read, Card, :user_id => user.id
|
|
|
|
can :read, Certification
|
2013-02-01 10:37:30 +00:00
|
|
|
can :read_details, Mac
|
|
|
|
can [:update], Mac, :user_id => nil
|
|
|
|
can [:create,:update], Mac, :user_id => user.id
|
2013-01-26 01:21:42 +00:00
|
|
|
can :read, User, :id => user.id #TODO: why can users update themselves?
|
2013-01-25 13:01:02 +00:00
|
|
|
can :read, UserCertification, :user_id => user.id
|
|
|
|
|
|
|
|
# Admins can manage all
|
2012-09-16 01:41:55 +00:00
|
|
|
if user.admin?
|
|
|
|
can :manage, :all
|
2012-09-04 06:20:00 +00:00
|
|
|
end
|
2013-01-25 13:01:02 +00:00
|
|
|
# Instructors can manage certs and see users
|
2013-01-25 10:12:25 +00:00
|
|
|
if user.instructor?
|
|
|
|
can :manage, Certification
|
2013-01-26 09:21:41 +00:00
|
|
|
can [:create,:read], User, :hidden => [nil,false]
|
2013-01-25 13:01:02 +00:00
|
|
|
can :manage, UserCertification
|
2013-01-25 10:12:25 +00:00
|
|
|
end
|
2013-01-25 13:01:02 +00:00
|
|
|
# Users can see others' stuff if they've been oriented
|
|
|
|
unless user.orientation.blank?
|
2013-01-25 20:47:44 +00:00
|
|
|
can :read, User, :hidden => [nil,false]
|
2013-01-25 13:01:02 +00:00
|
|
|
can :read, UserCertification
|
|
|
|
end
|
2013-01-26 01:21:42 +00:00
|
|
|
|
|
|
|
# Prevent all destruction for now
|
|
|
|
cannot :destroy, User
|
|
|
|
cannot :destroy, Card
|
|
|
|
cannot :destroy, Certification
|
2013-02-01 10:37:30 +00:00
|
|
|
cannot :destroy, Mac
|
|
|
|
cannot :destroy, MacLog
|
2013-01-26 01:21:42 +00:00
|
|
|
cannot :destroy, UserCertification
|
|
|
|
cannot :destroy, DoorLog
|
2012-09-16 01:41:55 +00:00
|
|
|
end
|
2012-09-02 11:37:34 +00:00
|
|
|
# Define abilities for the passed in user here. For example:
|
|
|
|
#
|
|
|
|
# user ||= User.new # guest user (not logged in)
|
|
|
|
# if user.admin?
|
|
|
|
# can :manage, :all
|
|
|
|
# else
|
|
|
|
# can :read, :all
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
end
|
|
|
|
end
|