-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathParseRole.php
120 lines (109 loc) · 2.93 KB
/
ParseRole.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Class ParseRole | Parse/ParseRole.php
*/
namespace Parse;
/**
* Class ParseRole - Representation of an access Role.
*
* @author Fosco Marotto <fjm@fb.com>
* @package Parse
*/
class ParseRole extends ParseObject
{
/**
* Parse Class name
*
* @var string
*/
public static $parseClassName = '_Role';
/**
* Create a ParseRole object with a given name and ACL.
*
* @param string $name
* @param ParseACL $acl
*
* @return ParseRole
*/
public static function createRole($name, ParseACL $acl)
{
$role = new ParseRole();
$role->setName($name);
$role->setACL($acl);
return $role;
}
/**
* Returns the role name.
*
* @return string
*/
public function getName()
{
return $this->get('name');
}
/**
* Sets the role name.
*
* @param string $name The role name
*
* @throws ParseException
*/
public function setName($name)
{
if ($this->getObjectId()) {
throw new ParseException("A role's name can only be set before it has been saved.");
}
if (!is_string($name)) {
throw new ParseException("A role's name must be a string.", 139);
}
return $this->set('name', $name);
}
/**
* Gets the ParseRelation for the ParseUsers which are direct children of
* this role. These users are granted any privileges that this role
* has been granted.
*
* @return ParseRelation
*/
public function getUsers()
{
return $this->getRelation('users');
}
/**
* Gets the ParseRelation for the ParseRoles which are direct children of
* this role. These roles' users are granted any privileges that this role
* has been granted.
*
* @return ParseRelation
*/
public function getRoles()
{
return $this->getRelation('roles');
}
/**
* Handles pre-saving of this role
* Calls ParseObject::save to finish
*
* @param bool $useMasterKey
* @throws ParseException
*/
public function save($useMasterKey = false)
{
if (!$this->getACL()) {
throw new ParseException('Roles must have an ACL.', 123);
}
if (!$this->getName() || !is_string($this->getName())) {
throw new ParseException('Roles must have a name.', 139);
}
if ($this->getObjectId() === null) {
// Not yet saved, verify this name is not taken
// ParseServer does not validate duplicate role names as of parse-server v2.3.2
$query = new ParseQuery('_Role');
$query->equalTo('name', $this->getName());
if ($query->count(true) > 0) {
throw new ParseException("Cannot add duplicate role name of '{$this->getName()}'", 137);
}
}
return parent::save($useMasterKey);
}
}