Module: Mongoid::ACL::Methods::InstanceMethods

Defined in:
lib/mongoid_acl/methods.rb

Instance Method Summary collapse

Instance Method Details

#can_destroy?(identifier) ⇒ Boolean

check if an actor with identifier has the destroy permission (Mongoid::ACL::DESTROY_PERM) on this object

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


25
26
27
# File 'lib/mongoid_acl/methods.rb', line 25

def can_destroy?(identifier)
  self.has_permission_for(Mongoid::ACL::DESTROY_PERM,identifier)
end

#can_manage?(identifier) ⇒ Boolean

quickly check whether an actor has the read,update and destroy permission on this object

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


32
33
34
# File 'lib/mongoid_acl/methods.rb', line 32

def can_manage?(identifier)
  can_read?(identifier) && can_update?(identifier) && can_destroy?(identifier)
end

#can_read?(identifier) ⇒ Boolean

check if an actor with identifier has read permission (Mongoid::ACL::READ_PERM) on this object

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


10
11
12
# File 'lib/mongoid_acl/methods.rb', line 10

def can_read?(identifier)
  self.has_permission_for(Mongoid::ACL::READ_PERM,identifier)
end

#can_update?(identifier) ⇒ Boolean

check if an actor with identifier has the update permission (Mongoid::ACL::UPDATE_PERM) on this object

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/mongoid_acl/methods.rb', line 17

def can_update?(identifier)
  self.has_permission_for(Mongoid::ACL::UPDATE_PERM,identifier)

end

#grant_destroy!(identifier) ⇒ Boolean

quickly add destroy permission for this actor

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


53
54
55
# File 'lib/mongoid_acl/methods.rb', line 53

def grant_destroy!(identifier)
  self.grant_permission_to(Mongoid::ACL::DESTROY_PERM,identifier)
end

#grant_manage!(identifier) ⇒ Boolean

quickly add read,update and destroy permission for this actor

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


61
62
63
# File 'lib/mongoid_acl/methods.rb', line 61

def grant_manage!(identifier)
  self.grant_permission_to([Mongoid::ACL::READ_PERM,Mongoid::ACL::UPDATE_PERM,Mongoid::ACL::DESTROY_PERM],identifier)
end

#grant_permission_to(permission, identifier) ⇒ Object

add identifier(s) to the given permission(s) in the acl list of this object

Parameters:

  • permission (Array, String)
  • identifier (Array, String)


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

def grant_permission_to(permission,identifier)
  if identifier.kind_of?(Array)
    identifier = {"$each" => identifier}
  end
  if permission.kind_of?(Array)
    hash_map = Hash.new
    permission.each{ |p| hash_map["acls.#{p}"] = identifier}
  else
    hash_map = {"acls.#{permission}" => identifier}
  end
  return self.collection.update({"_id" => self.id}, {"$addToSet" => hash_map })
end

#grant_read!(identifier) ⇒ Boolean

quickly add read permission for this actor

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


39
40
41
# File 'lib/mongoid_acl/methods.rb', line 39

def grant_read!(identifier)
  self.grant_permission_to(Mongoid::ACL::READ_PERM,identifier)
end

#grant_update!(identifier) ⇒ Boolean

quickly add update permission for this actor

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


46
47
48
# File 'lib/mongoid_acl/methods.rb', line 46

def grant_update!(identifier)
  self.grant_permission_to(Mongoid::ACL::UPDATE_PERM,identifier)
end

#has_permission_for(permission, identifier) ⇒ Object



136
137
138
139
# File 'lib/mongoid_acl/methods.rb', line 136

def has_permission_for(permission,identifier)
  return false if self.acls.nil?
  self.acls[permission].include?(PUBLIC_IDENTIFIER) || self.acls[permission].include?(identifier)
end

#revoke_all_permissionsBoolean

quickly remove all permissions

Returns:

  • (Boolean)


97
98
99
# File 'lib/mongoid_acl/methods.rb', line 97

def revoke_all_permissions
  self.collection.update({"_id" => self.id}, {"$unset" => {"acls"=>1} })
end

#revoke_destroy!(identifier) ⇒ Boolean

quickly remove destroy permission for this actor

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


82
83
84
# File 'lib/mongoid_acl/methods.rb', line 82

def revoke_destroy!(identifier)
  self.revoke_permission_for(Mongoid::ACL::DESTROY_PERM,identifier)
end

#revoke_manage!(identifier) ⇒ Boolean

quickly remove read,update and destroy permission for this actor

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


90
91
92
# File 'lib/mongoid_acl/methods.rb', line 90

def revoke_manage!(identifier)
  self.revoke_permission_for([Mongoid::ACL::READ_PERM,Mongoid::ACL::UPDATE_PERM,Mongoid::ACL::DESTROY_PERM],identifier)
end

#revoke_permission_for(permission, identifier) ⇒ Object

revoke identifier(s) from the given permission(s) in the acl list of this object

Parameters:

  • permission (Array, String)
  • identifier (Array, String)


122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mongoid_acl/methods.rb', line 122

def revoke_permission_for(permission,identifier)
  if !identifier.respond_to?('each')
    identifier = [identifier]
  end
  if permission.kind_of?(Array)
    hash_map = Hash.new
    permission.each{ |p| hash_map["acls.#{p}"] = identifier}
  else
    hash_map = {"acls.#{permission}" => identifier}
  end
  return self.collection.update({"_id" => self.id}, {"$pullAll" => hash_map })
end

#revoke_read(identifier) ⇒ Boolean

quickly remove read permission for this actor

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


68
69
70
# File 'lib/mongoid_acl/methods.rb', line 68

def revoke_read(identifier)
  self.revoke_permission_for(Mongoid::ACL::READ_PERM,identifier)
end

#revoke_update!(identifier) ⇒ Boolean

quickly remove update permission for this actor

Parameters:

  • identifier (String)

    of the actor

Returns:

  • (Boolean)


75
76
77
# File 'lib/mongoid_acl/methods.rb', line 75

def revoke_update!(identifier)
  self.revoke_permission_for(Mongoid::ACL::UPDATE_PERM,identifier)
end