permissionable.rb revision 4b8357d9eb7f8dda17322add358a476bde2135d5
included do
end
end
end
# Deny if user is nil
# Deny if user is of wrong type
# Allow any admin user.
# Retrieve direct user permissions.
# Retrieve permissions through team.
user.teams.each do |team|
@perms << Permission.item(self).subject(team).all
end
# Deny if no permission is found.
return false unless @perms
# Allow if role matches any permission.
@perms.flatten.each do |perm|
# Requested role exists exactly as permission.
return true if perm.role == role.to_s
# editors have reader permissions.
return true if perm.role == 'editor' and role == :reader
# owners have reader and editor permissions.
return true if perm.role == 'owner' and role == :reader
return true if perm.role == 'owner' and role == :editor
end
# Deny otherwise.
false
end
protected
def create_permission?
respond_to?(:user) && user
end
def add_permission
permissions.where(subject_id: user, subject_type: user.class).
first_or_create!(:subject => self.user, :role => 'owner')
end
end