2011-11-15 20:05:08 +00:00
|
|
|
class Ability
|
|
|
|
include CanCan::Ability
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
user ||= User.new # guest user (not logged in)
|
2011-11-16 18:45:01 +00:00
|
|
|
|
2011-11-15 20:05:08 +00:00
|
|
|
if user.admin?
|
|
|
|
can :manage, :all
|
|
|
|
else
|
2011-11-17 19:34:02 +00:00
|
|
|
#WARNING:
|
|
|
|
# - put cannot rules _after_ can rules and not before!
|
|
|
|
# - beware inner joins. Use sub queries against them!
|
2011-11-15 20:05:08 +00:00
|
|
|
# Shared rights between guests and registered users
|
2011-11-16 18:45:01 +00:00
|
|
|
can :forbidden, Platform
|
2011-11-17 19:34:02 +00:00
|
|
|
|
|
|
|
#cannot :read, Platform, :visibility => 'hidden'
|
|
|
|
can :read, [Project, Platform], :visibility => 'open'
|
|
|
|
|
2011-11-15 20:05:08 +00:00
|
|
|
# Guest rights
|
|
|
|
if user.guest?
|
2011-11-17 19:34:02 +00:00
|
|
|
can :create, User
|
|
|
|
|
2011-11-15 20:05:08 +00:00
|
|
|
# Registered user rights
|
|
|
|
else
|
|
|
|
# If rules goes one by one CanCan joins them by 'OR' sql operator
|
|
|
|
can :read, Project, :visibility => 'open'
|
2011-11-17 19:34:02 +00:00
|
|
|
# User can read and edit his profile:
|
|
|
|
can :manage, User, :id => user.id
|
2011-11-18 18:26:22 +00:00
|
|
|
can :manage_collaborators, Project do |project|
|
|
|
|
project.relations.exists? :object_id => user.id, :object_type => 'User', :role => 'admin'
|
|
|
|
end
|
2011-11-17 19:34:02 +00:00
|
|
|
|
|
|
|
# If rule has multiple conditions CanCan joins them by 'AND' sql operator
|
2011-11-18 18:26:22 +00:00
|
|
|
can [:read, :update, :process_build, :build, :destroy], Project, :owner_type => 'User', :owner_id => user.id
|
2011-11-17 19:34:02 +00:00
|
|
|
#can :read, Project, :relations => {:role => 'read'}
|
2011-11-18 18:26:22 +00:00
|
|
|
can :read, Project, projects_in_relations_with(:role => 'read', :object_type => 'User', :object_id => user.id) do |project|
|
|
|
|
#The can? and cannot? call cannot be used with a raw sql 'can' definition.
|
|
|
|
project.relations.exists?(:role => 'read', :object_type => 'User', :object_id => user.id)
|
|
|
|
end
|
2011-11-17 19:34:02 +00:00
|
|
|
#can [:update, :process_build, :build], Project, :relations => {:role => 'write'}
|
2011-11-18 18:26:22 +00:00
|
|
|
can [:read, :update, :process_build, :build], Project, projects_in_relations_with(:role => ['write', 'admin'], :object_type => 'User', :object_id => user.id) do |project|
|
|
|
|
project.relations.exists?(:role => ['write', 'admin'], :object_type => 'User', :object_id => user.id)
|
|
|
|
end
|
2011-11-16 18:45:01 +00:00
|
|
|
|
2011-11-17 19:34:02 +00:00
|
|
|
can :read, Platform, :owner_type => 'User', :owner_id => user.id
|
|
|
|
#can :read, Platform, :members => {:id => user.id}
|
2011-11-18 18:26:22 +00:00
|
|
|
can :read, Platform, platforms_in_relations_with(:role => 'read', :object_type => 'User', :object_id => user.id) do |platform|
|
|
|
|
platform.relations.exists?(:role => 'read', :object_type => 'User', :object_id => user.id)
|
|
|
|
end
|
2011-11-16 18:45:01 +00:00
|
|
|
|
|
|
|
#can :read, Repository
|
|
|
|
# TODO: Add personal repos rules
|
|
|
|
|
|
|
|
# Same rights for groups:
|
2011-11-18 18:26:22 +00:00
|
|
|
can [:read, :update, :process_build, :build, :destroy], Project, :owner_type => 'Group', :owner_id => user.group_ids
|
2011-11-17 19:34:02 +00:00
|
|
|
#can :read, Project, :relations => {:role => 'read', :object_type => 'Group', :object_id => user.group_ids}
|
2011-11-18 18:26:22 +00:00
|
|
|
can :read, Project, projects_in_relations_with(:role => 'read', :object_type => 'Group', :object_id => user.group_ids) do |project|
|
|
|
|
project.relations.exists?(:role => 'read', :object_type => 'Group', :object_id => user.group_ids)
|
|
|
|
end
|
2011-11-17 19:34:02 +00:00
|
|
|
#can [:update, :process_build, :build], Project, :relations => {:role => 'write', :object_type => 'Group', :object_id => user.group_ids}
|
2011-11-18 18:26:22 +00:00
|
|
|
can [:read, :update, :process_build, :build], Project, projects_in_relations_with(:role => ['write', 'admin'], :object_type => 'Group', :object_id => user.group_ids) do |project|
|
|
|
|
project.relations.exists?(:role => ['write', 'admin'], :object_type => 'Group', :object_id => user.group_ids)
|
|
|
|
end
|
2011-11-16 18:45:01 +00:00
|
|
|
|
|
|
|
can :manage, Platform, :owner_type => 'Group', :owner_id => user.group_ids
|
2011-11-17 19:34:02 +00:00
|
|
|
#can :read, Platform, :groups => {:id => user.group_ids}
|
2011-11-18 18:26:22 +00:00
|
|
|
can :read, Platform, platforms_in_relations_with(:role => 'read', :object_type => 'Group', :object_id => user.group_ids) do |platform|
|
|
|
|
platform.relations.exists?(:role => 'read', :object_type => 'Group', :object_id => user.group_ids)
|
|
|
|
end
|
2011-11-15 20:05:08 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-16 18:45:01 +00:00
|
|
|
|
|
|
|
# Shared rights for all users (guests, registered, admin)
|
|
|
|
cannot :destroy, Platform, :platform_type => 'personal'
|
2011-11-15 20:05:08 +00:00
|
|
|
end
|
2011-11-17 19:34:02 +00:00
|
|
|
|
|
|
|
# Sub query for platforms, projects relations
|
|
|
|
%w[platforms projects repositories].each do |table_name|
|
|
|
|
define_method table_name + "_in_relations_with" do |opts|
|
|
|
|
query = "#{ table_name }.id IN (SELECT target_id FROM relations WHERE relations.target_type = '#{ table_name.singularize.camelize }'"
|
|
|
|
opts.each do |key, value|
|
|
|
|
query = query + " AND relations.#{ key } #{ value.class == Array ? 'IN (?)' : '= ?' } "
|
|
|
|
end
|
|
|
|
query = query + ")"
|
|
|
|
|
|
|
|
return opts.values.unshift query
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
## Sub query for project relations
|
|
|
|
#def projects_in_relations_with(opts={})
|
|
|
|
# ["projects.id IN (SELECT target_id FROM relations WHERE relations.object_id #{ opts[:object_id].class == Array ? 'IN (?)' : '= ?' } AND relations.object_type = '#{ opts[:object_type] }' AND relations.target_type = 'Platform') AND relations.role", opts[:object_id]]
|
|
|
|
#end
|
2011-11-15 20:05:08 +00:00
|
|
|
end
|