permissionable.rb revision cd95118c2f49896d18b3c85236742fef7b655960
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.create! :subject => self.user, :role => 'owner'
end
end