Module: Cream::Helper::Role

Defined in:
lib/cream/helper/role.rb,
lib/cream/configure/after_init/role_config.rb

Defined Under Namespace

Modules: Labels

Instance Method Summary collapse

Instance Method Details

#for_any_role(*user_roles, &block) ⇒ Object Also known as: when_user_is_any_of



85
86
87
88
# File 'lib/cream/helper/role.rb', line 85

def for_any_role *user_roles, &block
  user_roles = user_roles.flat_uniq
  yield if has_any_role?(user_roles) && block
end

#for_any_user(options = nil, &block) ⇒ Object

for_any_user :signed_in for_any_user :not_logged_in

for_any_user :not_logged_in => true



8
9
10
# File 'lib/cream/helper/role.rb', line 8

def for_any_user options = nil, &block
  yield if Labels.state_check Labels.extract(options), current_user
end

#for_role(user_role, &block) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/cream/helper/role.rb', line 91

def for_role user_role, &block     
  if is_negation_role?(user_role) 
    not_for_role(user_role, &block)  
    return
  end
  yield if has_role?(user_role) && block
end

#for_roles(*user_roles, &block) ⇒ Object Also known as: when_user_is

execute block if user DOES have any of the given roles



79
80
81
82
# File 'lib/cream/helper/role.rb', line 79

def for_roles *user_roles, &block
  user_roles = user_roles.flat_uniq
  yield if has_roles?(user_roles) && block
end

#for_user_in_any_group(*names, &block) ⇒ Object



49
50
51
# File 'lib/cream/helper/role.rb', line 49

def for_user_in_any_group *names, &block
  yield if current_user && current_user.is_in_any_groups?(names.flat_uniq)
end

#for_user_in_group(name, &block) ⇒ Object

using group membership as guard



41
42
43
# File 'lib/cream/helper/role.rb', line 41

def for_user_in_group name, &block
  yield if current_user && current_user.is_in_group?(name)
end

#for_user_in_groups(*names, &block) ⇒ Object



45
46
47
# File 'lib/cream/helper/role.rb', line 45

def for_user_in_groups *names, &block
  yield if current_user && current_user.is_in_group?(names.flat_uniq)
end

#has_any_role?(user_role) ⇒ Boolean

does the user have ANY of the given roles? Uses generic roles API

Returns:

  • (Boolean)


29
30
31
# File 'lib/cream/helper/role.rb', line 29

def has_any_role? user_role
  current_user && current_user.has_any_role?(user_role)
end

#has_role?(user_role) ⇒ Boolean

does the user have ANY of the given roles? Uses generic roles API

Returns:

  • (Boolean)


23
24
25
# File 'lib/cream/helper/role.rb', line 23

def has_role? user_role
  current_user && current_user.has_role?(user_role)
end

#has_roles?(*roles) ⇒ Boolean

does the user have ALL of the given roles? Uses generic roles API

Returns:

  • (Boolean)


35
36
37
# File 'lib/cream/helper/role.rb', line 35

def has_roles? *roles
  current_user && current_user.has_roles?(roles.flat_uniq)
end

#not_for_any_user(options = nil, &block) ⇒ Object

not_for_any_user :signed_in not_for_any_user :logged_out not_for_any_user :logged_in => true



16
17
18
19
# File 'lib/cream/helper/role.rb', line 16

def not_for_any_user options = nil, &block
  return if Labels.state_check Labels.extract(options), current_user
  yield 
end

#not_for_role(user_role, &block) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/cream/helper/role.rb', line 106

def not_for_role(user_role, &block)
  if is_negation_role?(user_role) 
    for_role(user_role, &block)  
    return
  end
  yield if !has_role?(user_role) && block
end

#not_for_roles(*user_roles, &block) ⇒ Object Also known as: when_user_is_not

execute block if user DOES NOT have any of the given roles



101
102
103
104
# File 'lib/cream/helper/role.rb', line 101

def not_for_roles(*user_roles, &block)            
  user_roles = user_roles.flat_uniq
  yield if !has_roles?(user_roles) && block
end

#not_for_user_in_any_group(*names, &block) ⇒ Object



61
62
63
# File 'lib/cream/helper/role.rb', line 61

def not_for_user_in_any_group *names, &block
  yield if current_user && !current_user.is_in_any_groups?(names.flat_uniq)
end

#not_for_user_in_group(name, &block) ⇒ Object



53
54
55
# File 'lib/cream/helper/role.rb', line 53

def not_for_user_in_group name, &block
  yield if current_user && !current_user.is_in_group?(name)
end

#not_for_user_in_groups(*names, &block) ⇒ Object



57
58
59
# File 'lib/cream/helper/role.rb', line 57

def not_for_user_in_groups *names, &block
  yield if current_user && !current_user.is_in_group?(names.flat_uniq)
end

#owner?(obj, relation = nil) ⇒ Boolean

returns true if the current user owns the object tries default ‘owner’ relations if none given as an argument

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'lib/cream/helper/role.rb', line 68

def owner? obj, relation=nil
  if relation     
    return true if user_relation?(obj, relation)
  end                                
  [:user, :owner, :author].each do |relation|        
    return true if user_relation?(obj, relation)
  end
  false
end