Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit 0aa98f8

Browse files
committed
Change permission value
1 parent 1704579 commit 0aa98f8

File tree

2 files changed

+97
-33
lines changed

2 files changed

+97
-33
lines changed

core/src/core/classes/class.AJXP_Permission.php

100644100755
Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,30 @@
2424

2525
class AJXP_Permission
2626
{
27-
const READ = "r";
28-
const WRITE = "w";
29-
const DENY = "d";
27+
/**
28+
* Use an integer number to store permission
29+
* |r|w|d|t|....|..
30+
* |0|1|0|0|....|..
31+
*/
3032

31-
private $value = array(
32-
self::WRITE => false,
33-
self::READ => false,
34-
self::DENY => false
35-
);
33+
const MASK = 15; // 0000..001111 means use 4 first bits for encoding permission. The rest will be cut off.
34+
const READ = 1; // 0001
35+
const WRITE = 2; // 0010
36+
const DENY = 4; // 0100
37+
const TRAVEL = 8; //1000
38+
39+
private $value = 0;
3640

3741
/**
3842
* @param array|null $value
3943
*/
4044
function __construct($value = null){
4145
if($value != null){
4246
if(is_array($value)) $this->value = $value;
43-
else if(is_string($value)){
47+
elseif(is_integer($value)) {
48+
$this->value = $value & self::MASK;
49+
}
50+
elseif(is_string($value)){
4451
if(strpos($value, "r") !== false) $this->setRead();
4552
if(strpos($value, "w") !== false) $this->setWrite();
4653
if(strpos($value, "d") !== false) $this->setDeny();
@@ -56,47 +63,56 @@ function getCopy(){
5663
* @return bool
5764
*/
5865
function canRead(){
59-
return $this->value[self::READ];
66+
return ($this->value & self::READ) === self::READ;
6067
}
6168

6269
/**
6370
* @return bool
6471
*/
6572
function canWrite(){
66-
return $this->value[self::WRITE];
73+
return ($this->value & self::WRITE) === self::WRITE;
6774
}
6875

6976
/**
7077
* @return bool
7178
*/
7279
function denies(){
73-
if($this->value[self::DENY]) return true;
74-
if(!$this->value[self::DENY] && !$this->value[self::READ] && !$this->value[self::WRITE]){
75-
return true;
76-
}
80+
if ($this->value === self::DENY) return true;
81+
if ($this->value === 0) return true;
7782
return false;
7883
}
7984

80-
function testPermission($stringPerm){
81-
if($stringPerm == self::READ) return $this->canRead();
82-
else if($stringPerm == self::WRITE) return $this->canWrite();
85+
function testPermission($numPerm){
86+
if(is_integer($numPerm) && ($numPerm < 15)){
87+
$numPerm = $numPerm & self::MASK;
88+
if (($this->value !== 0) && $numPerm === 0) return false;
89+
if (($this->value === 0) && $numPerm === self::DENY) return true;
90+
return (($this->value & $numPerm) === $numPerm);
91+
}
8392
else{
84-
throw new Exception("Unimplemented permission : " . $stringPerm);
93+
throw new Exception("Unimplemented permission : " . $numPerm);
8594
}
8695
}
8796

8897
function setRead($value = true){
89-
$this->value[self::READ] = $value;
98+
if($value)
99+
$this->value = $this->value | self::READ;
100+
else{
101+
$this->value = $this->value & (self::READ ^ self::MASK);
102+
}
90103
}
91104
function setWrite($value = true){
92-
$this->value[self::WRITE] = $value;
105+
if($value)
106+
$this->value = $this->value | self::WRITE;
107+
else{
108+
$this->value = $this->value & (self::WRITE ^ self::MASK);
109+
}
93110
}
94111
function setDeny($value = true){
95-
if($value){
96-
$this->value[self::WRITE] = $this->value[self::READ] = false;
97-
$this->value[self::DENY] = true;
98-
}else{
99-
$this->value[self::DENY] = false;
112+
if($value)
113+
$this->value = $this->value & self::DENY;
114+
else{
115+
$this->value = $this->value & (self::DENY ^ self::MASK);
100116
}
101117
}
102118

@@ -109,8 +125,10 @@ function override($perm){
109125
if($this->denies()){
110126
$newPerm->setDeny();
111127
}else{
112-
if($this->canRead()) $newPerm->setRead();
113-
if($this->canWrite()) $newPerm->setWrite();
128+
if($this->canRead())
129+
$newPerm->setRead();
130+
if($this->canWrite())
131+
$newPerm->setWrite();
114132
}
115133
return $newPerm;
116134
}
@@ -126,5 +144,4 @@ function __toString(){
126144
return "READ WRITE";
127145
}
128146
}
129-
130147
}

core/src/core/classes/class.AJXP_PermissionMask.php

100644100755
Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ class AJXP_PermissionMask
2929
*/
3030
private $permissionTree;
3131

32-
function __construct(){}
32+
function __construct($path = null){
33+
/*if(!empty($path)) {
34+
$permission = new AJXP_Permission();
35+
$this->permissionTree = $this->pathToBranch($path, $permission);
36+
}
37+
*/
38+
}
3339

3440
/**
3541
* @return array
@@ -77,6 +83,15 @@ function override($mask){
7783
return $this;
7884
}
7985

86+
/**
87+
* @param AJXP_PermissionMask $mask
88+
* @return AJXP_PermissionMask
89+
*/
90+
91+
function copyMask($mask){
92+
$this->updateTree($mask->getTree());
93+
}
94+
8095
/**
8196
* @param string $test
8297
* @param string $permission
@@ -86,17 +101,17 @@ function match($test, $permission){
86101
// Check if a path has the given permission
87102

88103
$pathes = $this->flattenTree();
89-
//var_dump($pathes);
104+
//print_r($pathes);
90105
foreach($pathes as $path => $permObject){
91106
if(strpos($test, $path) === 0){
92-
var_dump("Test $test starts with existing path ".$path.":".$permObject);
107+
// var_dump("Test $test starts with existing path ".$path.":".$permObject);
93108
return $permObject->testPermission($permission);
94109
}
95110
}
96111
// test is not under a defined permission, check if it needs traversal
97112
foreach($pathes as $path => $permObject){
98113
if(strpos($path, $test) === 0 && !$permObject->denies()){
99-
var_dump("Existing path starts with test ($test) >> ".$path.":".$permObject);
114+
// var_dump("Existing path starts with test ($test) >> ".$path.":".$permObject);
100115
return $permObject->testPermission($permission);
101116
}
102117
}
@@ -144,6 +159,11 @@ private function mergeTrees($t1, $t2){
144159
return $result;
145160
}
146161

162+
163+
private function mergeTrees2($t1, $t2){
164+
165+
166+
}
147167
/**
148168
* @param string $path
149169
* @param AJXP_Permission $permission
@@ -186,4 +206,31 @@ private function flattenTree($tree = null, &$pathes = null, $currentRoot=""){
186206
return $pathes;
187207
}
188208

209+
210+
211+
public function toStr($permissionTree, $level)
212+
{
213+
$level = $level + 1;
214+
foreach ($permissionTree as $key => $node) {
215+
216+
$this->printSpace($level);
217+
echo "[" . $key . "]";
218+
if ($node instanceof AJXP_Permission) echo "(".$node.")";
219+
220+
echo "\n";
221+
if (is_array($node) && count($node) > 0) {
222+
$permissionTree = $node;
223+
$this->toStr($permissionTree, $level);
224+
}
225+
226+
}
227+
$level = $level - 1;
228+
//echo "\n";
229+
}
230+
231+
public function printSpace($number){
232+
for ($i = 0; $i < $number; $i++){
233+
echo "--";
234+
}
235+
}
189236
}

0 commit comments

Comments
 (0)