Skip to content

Commit

Permalink
add permission interfaces + implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
matthi4s committed Dec 6, 2024
1 parent 6f76ac6 commit e749a23
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/Interfaces/Util/PermissionsClassInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Aternos\IO\Interfaces\Util;

/**
* Interface PermissionsClassInterface
*
* Interface for class permissions
*
* @package Aternos\IO\Interfaces\Util
*/
interface PermissionsClassInterface
{
/**
* Check if the class has read permissions
*
* @return bool
*/
public function canRead(): bool;

/**
* Set the read permission of the class
*
* @param bool $read
* @return $this
*/
public function setRead(bool $read): static;

/**
* Check if the class has write permissions
*
* @return bool
*/
public function canWrite(): bool;

/**
* Set the write permission of the class
*
* @param bool $write
* @return $this
*/
public function setWrite(bool $write): static;

/**
* Check if the class has execute permissions
*
* @return bool
*/
public function canExecute(): bool;

/**
* Set the execute permission of the class
*
* @param bool $execute
* @return $this
*/
public function setExecute(bool $execute): static;
}
58 changes: 58 additions & 0 deletions src/Interfaces/Util/PermissionsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Aternos\IO\Interfaces\Util;

/**
* Interface PermissionsInterface
*
* Interface for element permissions by class
*
* @package Aternos\IO\Interfaces\Util
*/
interface PermissionsInterface
{
/**
* Get the user permissions
*
* @return PermissionsClassInterface
*/
public function getUser(): PermissionsClassInterface;

/**
* Set the user permissions
*
* @param PermissionsClassInterface $user
* @return $this
*/
public function setUser(PermissionsClassInterface $user): static;

/**
* Get the group permissions
*
* @return PermissionsClassInterface
*/
public function getGroup(): PermissionsClassInterface;

/**
* Set the group permissions
*
* @param PermissionsClassInterface $group
* @return $this
*/
public function setGroup(PermissionsClassInterface $group): static;

/**
* Get the other permissions
*
* @return PermissionsClassInterface
*/
public function getOther(): PermissionsClassInterface;

/**
* Set the other permissions
*
* @param PermissionsClassInterface $other
* @return $this
*/
public function setOther(PermissionsClassInterface $other): static;
}
106 changes: 106 additions & 0 deletions src/System/Util/Permissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Aternos\IO\System\Util;

use Aternos\IO\Interfaces\Util\PermissionsClassInterface;
use Aternos\IO\Interfaces\Util\PermissionsInterface;

/**
* Class Permissions
*
* Holds permission classes for an element
*
* @package Aternos\IO\System\Util
*/
class Permissions implements PermissionsInterface
{
/**
* @param int $permissions
* @return static
*/
public static function fromNumeric(int $permissions): static
{
return new static(
new PermissionsClass(
($permissions & 0o100) === 0o100,
($permissions & 0o200) === 0o200,
($permissions & 0o400) === 0o400
),
new PermissionsClass(
($permissions & 0o010) === 0o010,
($permissions & 0o020) === 0o020,
($permissions & 0o040) === 0o040
),
new PermissionsClass(
($permissions & 0o001) === 0o001,
($permissions & 0o002) === 0o002,
($permissions & 0o004) === 0o004
)
);
}


/**
* @param PermissionsClassInterface $user
* @param PermissionsClassInterface $group
* @param PermissionsClassInterface $other
*/
public function __construct(
protected PermissionsClassInterface $user = new PermissionsClass(),
protected PermissionsClassInterface $group = new PermissionsClass(),
protected PermissionsClassInterface $other = new PermissionsClass()
)
{
}

/**
* @inheritDoc
*/
public function getUser(): PermissionsClassInterface
{
return $this->user;
}

/**
* @inheritDoc
*/
public function getGroup(): PermissionsClassInterface
{
return $this->group;
}

/**
* @inheritDoc
*/
public function getOther(): PermissionsClassInterface
{
return $this->other;
}

/**
* @inheritDoc
*/
public function setUser(PermissionsClassInterface $user): static
{
$this->user = $user;
return $this;
}

/**
* @inheritDoc
*/
public function setGroup(PermissionsClassInterface $group): static
{
$this->group = $group;
return $this;
}

/**
* @inheritDoc
*/
public function setOther(PermissionsClassInterface $other): static
{
$this->other = $other;
return $this;
}
}
79 changes: 79 additions & 0 deletions src/System/Util/PermissionsClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Aternos\IO\System\Util;

use Aternos\IO\Interfaces\Util\PermissionsClassInterface;

/**
* Class PermissionsClass
*
* Holds permission values for a specific class
*
* @package Aternos\IO\System\Util
*/
class PermissionsClass implements PermissionsClassInterface
{
/**
* @param bool $read
* @param bool $write
* @param bool $execute
*/
public function __construct(
protected bool $read = false,
protected bool $write = false,
protected bool $execute = false
)
{
}

/**
* @inheritDoc
*/
public function canRead(): bool
{
return $this->read;
}

/**
* @inheritDoc
*/
public function canWrite(): bool
{
return $this->write;
}

/**
* @inheritDoc
*/
public function canExecute(): bool
{
return $this->execute;
}

/**
* @inheritDoc
*/
public function setRead(bool $read): static
{
$this->read = $read;
return $this;
}

/**
* @inheritDoc
*/
public function setWrite(bool $write): static
{
$this->write = $write;
return $this;
}

/**
* @inheritDoc
*/
public function setExecute(bool $execute): static
{
$this->execute = $execute;
return $this;
}
}
41 changes: 41 additions & 0 deletions test/Unit/System/Util/PermissionsClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Aternos\IO\Test\Unit\System\Util;

use Aternos\IO\System\Util\PermissionsClass;
use PHPUnit\Framework\TestCase;

class PermissionsClassTest extends TestCase
{
public function testGetPermissions(): void
{
$permissions = new PermissionsClass(true, false, true);
$this->assertTrue($permissions->canRead());
$this->assertFalse($permissions->canWrite());
$this->assertTrue($permissions->canExecute());
}

public function testSetRead(): void
{
$permissions = new PermissionsClass();
$this->assertFalse($permissions->canRead());
$permissions->setRead(true);
$this->assertTrue($permissions->canRead());
}

public function testSetWrite(): void
{
$permissions = new PermissionsClass();
$this->assertFalse($permissions->canWrite());
$permissions->setWrite(true);
$this->assertTrue($permissions->canWrite());
}

public function testSetExecute(): void
{
$permissions = new PermissionsClass();
$this->assertFalse($permissions->canExecute());
$permissions->setExecute(true);
$this->assertTrue($permissions->canExecute());
}
}
Loading

0 comments on commit e749a23

Please sign in to comment.