Module: Authorized::ModelMethods::InstanceMethods

Defined in:
lib/authorized.rb

Instance Method Summary collapse

Instance Method Details

#container_permissions(container_id, container) ⇒ Object

Method returning the workspace permissions list

Parameters :

  • workspace_id: Integer defining the workspace id

Usage : user.workspace_permissions(2)



69
70
71
72
73
74
75
# File 'lib/authorized.rb', line 69

def container_permissions(container_id, container)
	if @users_container = UsersContainer.find(:first, :conditions => {:user_id => self.id, :containerable_id => container_id, :containerable_type => container})
		return @permissions = @users_container.role.permissions
	else
		return []
	end
end

#has_container_permission(container_id, controller, action, container) ⇒ Object

Method returning true if user has the workspace permission, false else

Parameters :

  • workspace_id: Integer for workspace id

  • controller: String defining the controller defining the first part of the permission

  • action: String defining the action defining the second part of the permission

Usage : user.has_workspace_permission('workspace_id','articles','new')



99
100
101
102
# File 'lib/authorized.rb', line 99

def has_container_permission(container_id, controller, action, container)
	permission_name = controller+'_'+action
	return self.has_system_role('superadmin') || !self.container_permissions(container_id,container).delete_if{ |e| e.name != permission_name}.blank? 
end

#has_container_role(container_id, container, role_name) ⇒ Object

Method returning true if the user has the workspace role passed in params, false else

Parameters :

  • workspace_id: Integer for workspace id

  • role_name: String defining the role

Usage: user.has_workspace_role('ws_admin')



50
51
52
# File 'lib/authorized.rb', line 50

def has_container_role(container_id, container, role_name)
	UsersContainer.exists?(:user_id => self.id, :containerable_id => container_id, :containerable_type => container.capitalize, :role_id => Role.find_by_name(role_name).id) || self.system_role.name == 'superadmin'
end

#has_system_permission(controller, action) ⇒ Object

Method returning true if user has the system permission, false else

Parameters :

  • controller: String defining the controller defining the first part of the permission

  • action: String defining the action defining the second part of the permission

Usage : user.has_system_permission('workspaces','new')



85
86
87
88
# File 'lib/authorized.rb', line 85

def has_system_permission(controller, action)
	permission_name = controller+'_'+action
	return self.has_system_role('superadmin')|| !self.system_permissions.delete_if{ |e| e.name != permission_name}.blank?
end

#has_system_role(role_name) ⇒ Object

Method returning true if the user has the system role passed in params, false else

Parameters :

  • role_name: String defining the role

Usage : user.has_system_role('admin')



38
39
40
# File 'lib/authorized.rb', line 38

def has_system_role(role_name)
	(self.system_role.name == 'superadmin') || (self.system_role.name == role_name)
end

#system_permissionsObject

Method returning the system permissions list

Usage : user.system_permissions



58
59
60
# File 'lib/authorized.rb', line 58

def system_permissions
	@permissions ||= self.system_role.permissions
end

#system_roleObject

Method returning the system role

Usage : user.system_role



27
28
29
# File 'lib/authorized.rb', line 27

def system_role
	@role ||= Role.find(self.system_role_id)
end