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.



975
976
977
978
979
980
981
982
983
984
# File 'lib/parse/model/acl.rb', line 975

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

Instance Attribute Details

#read ⇒ Boolean (readonly)

The read permission state.

Returns:

  • (Boolean) —

    whether this permission is allowed.



937
938
939
# File 'lib/parse/model/acl.rb', line 937

def read
  @read
end

#write ⇒ Boolean (readonly)

The write permission state.

Returns:

  • (Boolean) —

    whether this permission is allowed.



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

def write
  @write
end

Instance Method Details

#==(per) ⇒ Boolean Also known as: eql?

Returns whether two permission instances have the same permissions.

Returns:

  • (Boolean) —

    whether two permission instances have the same permissions.



994
995
996
997
# File 'lib/parse/model/acl.rb', line 994

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



1008
1009
1010
1011
1012
1013
# File 'lib/parse/model/acl.rb', line 1008

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

#attributes ⇒ Hash

Returns:



1016
1017
1018
1019
1020
1021
# File 'lib/parse/model/acl.rb', line 1016

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

#hash ⇒ Integer

Returns a hash value consistent with #eql?.

Returns:

  • (Integer) —

    a hash value consistent with #eql?.



1002
1003
1004
# File 'lib/parse/model/acl.rb', line 1002

def hash
  [self.class, @read, @write].hash
end

#no_read!

This method returns an undefined value.

Sets the read value of the permission to false.

Version:

  • 1.7.2



1060
1061
1062
# File 'lib/parse/model/acl.rb', line 1060

def no_read!
  read!(false)
end

#no_write!

This method returns an undefined value.

Sets the write value of the permission to false.

Version:

  • 1.7.2



1067
1068
1069
# File 'lib/parse/model/acl.rb', line 1067

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.



1029
1030
1031
# File 'lib/parse/model/acl.rb', line 1029

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

#read!(value = true) ⇒ Boolean

Sets the read value of the permission. Defaults to true. When the permission belongs to an ACL, a real change marks that ACL (and its owning object) dirty so the change is sent on the next save.

Returns:

  • (Boolean) —

    the new read value.

Version:

  • 1.7.2



1038
1039
1040
1041
1042
1043
# File 'lib/parse/model/acl.rb', line 1038

def read!(value = true)
  value = self.class.grant?(value)
  return value if value == @read
  @acl&.will_change!
  @read = value
end

#write!(value = true) ⇒ Boolean

Sets the write value of the permission. Defaults to true. When the permission belongs to an ACL, a real change marks that ACL (and its owning object) dirty so the change is sent on the next save.

Returns:

  • (Boolean) —

    the new write value.

Version:

  • 1.7.2



1050
1051
1052
1053
1054
1055
# File 'lib/parse/model/acl.rb', line 1050

def write!(value = true)
  value = self.class.grant?(value)
  return value if value == @write
  @acl&.will_change!
  @write = value
end