Skip to content

Commit e749a23

Browse files
committed
add permission interfaces + implementation
1 parent 6f76ac6 commit e749a23

File tree

6 files changed

+439
-0
lines changed

6 files changed

+439
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Util;
4+
5+
/**
6+
* Interface PermissionsClassInterface
7+
*
8+
* Interface for class permissions
9+
*
10+
* @package Aternos\IO\Interfaces\Util
11+
*/
12+
interface PermissionsClassInterface
13+
{
14+
/**
15+
* Check if the class has read permissions
16+
*
17+
* @return bool
18+
*/
19+
public function canRead(): bool;
20+
21+
/**
22+
* Set the read permission of the class
23+
*
24+
* @param bool $read
25+
* @return $this
26+
*/
27+
public function setRead(bool $read): static;
28+
29+
/**
30+
* Check if the class has write permissions
31+
*
32+
* @return bool
33+
*/
34+
public function canWrite(): bool;
35+
36+
/**
37+
* Set the write permission of the class
38+
*
39+
* @param bool $write
40+
* @return $this
41+
*/
42+
public function setWrite(bool $write): static;
43+
44+
/**
45+
* Check if the class has execute permissions
46+
*
47+
* @return bool
48+
*/
49+
public function canExecute(): bool;
50+
51+
/**
52+
* Set the execute permission of the class
53+
*
54+
* @param bool $execute
55+
* @return $this
56+
*/
57+
public function setExecute(bool $execute): static;
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Util;
4+
5+
/**
6+
* Interface PermissionsInterface
7+
*
8+
* Interface for element permissions by class
9+
*
10+
* @package Aternos\IO\Interfaces\Util
11+
*/
12+
interface PermissionsInterface
13+
{
14+
/**
15+
* Get the user permissions
16+
*
17+
* @return PermissionsClassInterface
18+
*/
19+
public function getUser(): PermissionsClassInterface;
20+
21+
/**
22+
* Set the user permissions
23+
*
24+
* @param PermissionsClassInterface $user
25+
* @return $this
26+
*/
27+
public function setUser(PermissionsClassInterface $user): static;
28+
29+
/**
30+
* Get the group permissions
31+
*
32+
* @return PermissionsClassInterface
33+
*/
34+
public function getGroup(): PermissionsClassInterface;
35+
36+
/**
37+
* Set the group permissions
38+
*
39+
* @param PermissionsClassInterface $group
40+
* @return $this
41+
*/
42+
public function setGroup(PermissionsClassInterface $group): static;
43+
44+
/**
45+
* Get the other permissions
46+
*
47+
* @return PermissionsClassInterface
48+
*/
49+
public function getOther(): PermissionsClassInterface;
50+
51+
/**
52+
* Set the other permissions
53+
*
54+
* @param PermissionsClassInterface $other
55+
* @return $this
56+
*/
57+
public function setOther(PermissionsClassInterface $other): static;
58+
}

src/System/Util/Permissions.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Aternos\IO\System\Util;
4+
5+
use Aternos\IO\Interfaces\Util\PermissionsClassInterface;
6+
use Aternos\IO\Interfaces\Util\PermissionsInterface;
7+
8+
/**
9+
* Class Permissions
10+
*
11+
* Holds permission classes for an element
12+
*
13+
* @package Aternos\IO\System\Util
14+
*/
15+
class Permissions implements PermissionsInterface
16+
{
17+
/**
18+
* @param int $permissions
19+
* @return static
20+
*/
21+
public static function fromNumeric(int $permissions): static
22+
{
23+
return new static(
24+
new PermissionsClass(
25+
($permissions & 0o100) === 0o100,
26+
($permissions & 0o200) === 0o200,
27+
($permissions & 0o400) === 0o400
28+
),
29+
new PermissionsClass(
30+
($permissions & 0o010) === 0o010,
31+
($permissions & 0o020) === 0o020,
32+
($permissions & 0o040) === 0o040
33+
),
34+
new PermissionsClass(
35+
($permissions & 0o001) === 0o001,
36+
($permissions & 0o002) === 0o002,
37+
($permissions & 0o004) === 0o004
38+
)
39+
);
40+
}
41+
42+
43+
/**
44+
* @param PermissionsClassInterface $user
45+
* @param PermissionsClassInterface $group
46+
* @param PermissionsClassInterface $other
47+
*/
48+
public function __construct(
49+
protected PermissionsClassInterface $user = new PermissionsClass(),
50+
protected PermissionsClassInterface $group = new PermissionsClass(),
51+
protected PermissionsClassInterface $other = new PermissionsClass()
52+
)
53+
{
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
public function getUser(): PermissionsClassInterface
60+
{
61+
return $this->user;
62+
}
63+
64+
/**
65+
* @inheritDoc
66+
*/
67+
public function getGroup(): PermissionsClassInterface
68+
{
69+
return $this->group;
70+
}
71+
72+
/**
73+
* @inheritDoc
74+
*/
75+
public function getOther(): PermissionsClassInterface
76+
{
77+
return $this->other;
78+
}
79+
80+
/**
81+
* @inheritDoc
82+
*/
83+
public function setUser(PermissionsClassInterface $user): static
84+
{
85+
$this->user = $user;
86+
return $this;
87+
}
88+
89+
/**
90+
* @inheritDoc
91+
*/
92+
public function setGroup(PermissionsClassInterface $group): static
93+
{
94+
$this->group = $group;
95+
return $this;
96+
}
97+
98+
/**
99+
* @inheritDoc
100+
*/
101+
public function setOther(PermissionsClassInterface $other): static
102+
{
103+
$this->other = $other;
104+
return $this;
105+
}
106+
}

src/System/Util/PermissionsClass.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Aternos\IO\System\Util;
4+
5+
use Aternos\IO\Interfaces\Util\PermissionsClassInterface;
6+
7+
/**
8+
* Class PermissionsClass
9+
*
10+
* Holds permission values for a specific class
11+
*
12+
* @package Aternos\IO\System\Util
13+
*/
14+
class PermissionsClass implements PermissionsClassInterface
15+
{
16+
/**
17+
* @param bool $read
18+
* @param bool $write
19+
* @param bool $execute
20+
*/
21+
public function __construct(
22+
protected bool $read = false,
23+
protected bool $write = false,
24+
protected bool $execute = false
25+
)
26+
{
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function canRead(): bool
33+
{
34+
return $this->read;
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function canWrite(): bool
41+
{
42+
return $this->write;
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function canExecute(): bool
49+
{
50+
return $this->execute;
51+
}
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
public function setRead(bool $read): static
57+
{
58+
$this->read = $read;
59+
return $this;
60+
}
61+
62+
/**
63+
* @inheritDoc
64+
*/
65+
public function setWrite(bool $write): static
66+
{
67+
$this->write = $write;
68+
return $this;
69+
}
70+
71+
/**
72+
* @inheritDoc
73+
*/
74+
public function setExecute(bool $execute): static
75+
{
76+
$this->execute = $execute;
77+
return $this;
78+
}
79+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Aternos\IO\Test\Unit\System\Util;
4+
5+
use Aternos\IO\System\Util\PermissionsClass;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class PermissionsClassTest extends TestCase
9+
{
10+
public function testGetPermissions(): void
11+
{
12+
$permissions = new PermissionsClass(true, false, true);
13+
$this->assertTrue($permissions->canRead());
14+
$this->assertFalse($permissions->canWrite());
15+
$this->assertTrue($permissions->canExecute());
16+
}
17+
18+
public function testSetRead(): void
19+
{
20+
$permissions = new PermissionsClass();
21+
$this->assertFalse($permissions->canRead());
22+
$permissions->setRead(true);
23+
$this->assertTrue($permissions->canRead());
24+
}
25+
26+
public function testSetWrite(): void
27+
{
28+
$permissions = new PermissionsClass();
29+
$this->assertFalse($permissions->canWrite());
30+
$permissions->setWrite(true);
31+
$this->assertTrue($permissions->canWrite());
32+
}
33+
34+
public function testSetExecute(): void
35+
{
36+
$permissions = new PermissionsClass();
37+
$this->assertFalse($permissions->canExecute());
38+
$permissions->setExecute(true);
39+
$this->assertTrue($permissions->canExecute());
40+
}
41+
}

0 commit comments

Comments
 (0)