Module: MongoMapper::Plugins::AuthorizedObject::InstanceMethods

Defined in:
lib/rad/mongo_mapper/acts_as/authorized_object.rb

Instance Method Summary collapse

Instance Method Details

#able_update?(user) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 135

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

#able_view?(user) ⇒ Boolean

Special Permissions

Returns:

  • (Boolean)


131
132
133
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 131

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

#add_collaborator(role) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 97

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



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 55

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



87
88
89
90
91
92
93
94
95
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 87

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



117
118
119
120
121
122
123
124
125
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 117

def normalized_collaborators
  unless normalized_collaborators = cache[:normalized_collaborators]
    normalized_collaborators = Role.denormalize_to_higher_roles collaborators
    normalized_collaborators << "user:#{owner_name}"
    normalized_collaborators.sort!
    cache[:normalized_collaborators] = normalized_collaborators
  end
  normalized_collaborators
end

#ownerObject

Owner



22
23
24
25
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 22

def owner
  return nil if owner_name.blank?
  cache[:owner] ||= User.find_by_name! owner_name
end

#owner=(user) ⇒ Object



27
28
29
30
31
32
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 27

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

#owner_name=(name) ⇒ Object

TODO2 update it later, MM uses public API to unmarshal object groups.google.com/group/mongomapper/browse_thread/thread/ab34457e0ba9c472#



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 36

def owner_name= name                    
  owner_role = "user:#{name}"
  old_owner_role = "user:#{owner_name}"
  
  unless viewers.include? owner_role
    viewers.delete old_owner_role
    viewers << owner_role
    viewers.sort!
  end
  
  # write_attribute :owner_name, name
  super name
  clear_cache
  owner_name
end

#remove_collaborator(role) ⇒ Object



110
111
112
113
114
115
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 110

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

#remove_viewer(role) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rad/mongo_mapper/acts_as/authorized_object.rb', line 69

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