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.to_s).
first_or_create!(subject: user, role: 'owner')
end
end