Class: Parse::ACL::Permission

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Serializers::JSON
Defined in:
lib/parse/model/acl.rb

Overview

The Permission class tracks the read and write permissions for a specific ACL entry. The value of an Parse-ACL hash only contains two keys: “read” and “write”.

# Example of the ACL format
 { "*":          { "read": true },
   "3KmCvT7Zsb": { "read": true, "write": true },
   "role:Admins": { "write": true }
 }

This would be managed as:

{ "*":          ACL::Permission.new(true),
  "3KmCvT7Zsb": ACL::Permission.new(true, true)
  "role:Amdins": ACL::Permission.new(false,true)
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new(read = nil, write = nil) ⇒ Permission #new(hash) ⇒ Permission

Create a new permission with the given read and write privileges.

Overloads:

  • #new(read = nil, write = nil) ⇒ Permission

    Examples:

    ACL::Permission.new(true, false)

    Parameters:

    • read (Boolean) (defaults to: nil)

      whether reading is allowed.

    • write (Boolean) (defaults to: nil)

      whether writing is allowed.

  • #new(hash) ⇒ Permission

    Examples:

    ACL::Permission.new({read: true, write: false})

    Parameters:

    • hash (Hash)

      a key value pair for read/write permissions.



893
894
895
896
897
898
899
900
901
902
# File 'lib/parse/model/acl.rb', line 893

def initialize(r_perm = nil, w_perm = nil)
  if r_perm.is_a?(Hash)
    r_perm = r_perm.symbolize_keys
    @read = r_perm[:read].present?
    @write = r_perm[:write].present?
  else
    @read = r_perm.present?
    @write = w_perm.present?
  end
end

Instance Attribute Details

#readBoolean (readonly)

The read permission state.

Returns:

  • (Boolean)

    whether this permission is allowed.



876
877
878
# File 'lib/parse/model/acl.rb', line 876

def read
  @read
end

#writeBoolean (readonly)

The write permission state.

Returns:

  • (Boolean)

    whether this permission is allowed.



880
881
882
# File 'lib/parse/model/acl.rb', line 880

def write
  @write
end

Instance Method Details

#==(per) ⇒ Boolean

Returns whether two permission instances have the same permissions.

Returns:

  • (Boolean)

    whether two permission instances have the same permissions.



905
906
907
908
# File 'lib/parse/model/acl.rb', line 905

def ==(per)
  return false unless per.is_a?(self.class)
  @read == per.read && @write == per.write
end

#as_json(*args) ⇒ Hash

Returns A Parse-compatible ACL-hash. Omission or false on a priviledge means don’t include it.

Returns:

  • (Hash)

    A Parse-compatible ACL-hash. Omission or false on a priviledge means don’t include it



912
913
914
915
916
917
# File 'lib/parse/model/acl.rb', line 912

def as_json(*args)
  h = {}
  h[:read] = true if @read
  h[:write] = true if @write
  h.empty? ? nil : h.as_json
end

#attributesHash

Returns:



920
921
922
923
924
925
# File 'lib/parse/model/acl.rb', line 920

def attributes
  h = {}
  h.merge!(read: :boolean) if @read
  h.merge!(write: :boolean) if @write
  h
end

#no_read!void

This method returns an undefined value.

Sets the read value of the permission to false.

Version:

  • 1.7.2



956
957
958
# File 'lib/parse/model/acl.rb', line 956

def no_read!
  @read = false
end

#no_write!void

This method returns an undefined value.

Sets the write value of the permission to false.

Version:

  • 1.7.2



963
964
965
# File 'lib/parse/model/acl.rb', line 963

def no_write!
  @write = false
end

#present?Boolean

Returns whether there is at least one permission set to true.

Returns:

  • (Boolean)

    whether there is at least one permission set to true.



933
934
935
# File 'lib/parse/model/acl.rb', line 933

def present?
  @read.present? || @write.present?
end

#read!(value = true) ⇒ void

Note:

Setting the value in this manner is not dirty tracked.

This method returns an undefined value.

Sets the read value of the permission. Defaults to true.

Version:

  • 1.7.2



941
942
943
# File 'lib/parse/model/acl.rb', line 941

def read!(value = true)
  @read = value
end

#write!(value = true) ⇒ void

Note:

Setting the value in this manner is not dirty tracked.

This method returns an undefined value.

Sets the write value of the permission. Defaults to true.

Version:

  • 1.7.2



949
950
951
# File 'lib/parse/model/acl.rb', line 949

def write!(value = true)
  @write = value
end