Module: Models::AuthorizedObject

Defined in:
lib/kit/models/authorized_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collaboratorsObject



8
# File 'lib/kit/models/authorized_object.rb', line 8

def collaborators; @collaborators ||= [] end

#owner_nameObject



3
4
5
# File 'lib/kit/models/authorized_object.rb', line 3

def owner_name
  @owner_name ||= rad.user? ? rad.user.name : nil
end

#viewersObject



11
12
13
14
15
16
# File 'lib/kit/models/authorized_object.rb', line 11

def viewers
  @viewers ||= (
    (rad.user? ? ['manager', "user:#{rad.user.name}"] : ['manager']) +
    Array.wrap(rad.config.default_viewers)
  ).uniq.sort
end

Instance Method Details

#able_update?(user) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/kit/models/authorized_object.rb', line 142

def able_update? user
  user.roles.any?{|role| normalized_collaborators.include? role}
end

#able_view?(user) ⇒ Boolean

Special Permissions

Returns:

  • (Boolean)


138
139
140
# File 'lib/kit/models/authorized_object.rb', line 138

def able_view? user
  user.roles.any?{|role| viewers.include? role}
end

#add_collaborator(role) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kit/models/authorized_object.rb', line 105

def add_collaborator role
  role = role.to_s
  should_be_valid_user_input_role role
  return if collaborators.include? role
  collaborators = self.collaborators.clone
  collaborators << role
  self.collaborators = collaborators

  add_viewer role

  collaborators
end

#add_viewer(role) ⇒ Object

Viewers and Collaborators



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kit/models/authorized_object.rb', line 63

def add_viewer role
  role = role.to_s
  should_be_valid_user_input_role role

  return if viewers.include? role

  roles = viewers
  roles << role
  roles = Role.denormalize_to_higher_roles roles
  roles << 'manager' unless roles.include? 'manager'
  self.viewers = roles.sort
  viewers
end

#minor_viewersObject



95
96
97
98
99
100
101
102
103
# File 'lib/kit/models/authorized_object.rb', line 95

def minor_viewers
  unless minor_viewers = _cache[:minor_viewers]
    viewers = self.viewers.clone
    viewers.delete 'manager'
    minor_viewers = Role.minor_roles viewers
    _cache[:minor_viewers] = minor_viewers
  end
  minor_viewers
end

#normalized_collaboratorsObject



125
126
127
128
129
130
131
132
133
# File 'lib/kit/models/authorized_object.rb', line 125

def normalized_collaborators
  unless normalized_collaborators = _cache[:normalized_collaborators]
    normalized_collaborators = Role.denormalize_to_higher_roles collaborators
    normalized_collaborators << "user:#{owner_name}" unless Models::Authorized.anonymous?(owner_name)
    normalized_collaborators.sort!
    _cache[:normalized_collaborators] = normalized_collaborators
  end
  normalized_collaborators
end

#ownerObject

Owner



30
31
32
33
# File 'lib/kit/models/authorized_object.rb', line 30

def owner
  return nil if owner_name.blank?
  _cache[:owner] ||= Models::User.by_name!(owner_name)
end

#owner=(user) ⇒ Object



35
36
37
38
39
40
# File 'lib/kit/models/authorized_object.rb', line 35

def owner= user
  user.must_be.an Models::User
  _cache[:owner] = user
  self.owner_name = user.name
  user
end

#remove_collaborator(role) ⇒ Object



118
119
120
121
122
123
# File 'lib/kit/models/authorized_object.rb', line 118

def remove_collaborator role
  role = role.to_s
  should_be_valid_user_input_role role
  collaborators.delete role
  collaborators
end

#remove_viewer(role) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kit/models/authorized_object.rb', line 77

def remove_viewer role
  role = role.to_s
  should_be_valid_user_input_role role

  return unless viewers.include? role

  roles = viewers
  Role.denormalize_to_higher_roles([role]).each do |r|
    roles.delete r
  end
  roles << 'manager' unless roles.include? 'manager'
  self.viewers = roles.sort

  remove_collaborator role

  viewers
end