AAdd domain specific Exceptions for PermissionSet
This commit is contained in:
parent
4242539059
commit
a38abd3363
3 changed files with 104 additions and 40 deletions
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Security\PermissionSet\Exception;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class PermissionSetNotFoundException extends \RuntimeException
|
||||||
|
{
|
||||||
|
public function __construct($message = '', Exception $previous = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message, 500, $previous);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Security\PermissionSet\Exception;
|
||||||
|
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class PermissionSetPersistenceException extends \RuntimeException
|
||||||
|
{
|
||||||
|
public function __construct($message = "", Throwable $previous = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message, 500, $previous);
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,6 +27,8 @@ use Friendica\Database\Database;
|
||||||
use Friendica\Model\Contact;
|
use Friendica\Model\Contact;
|
||||||
use Friendica\Model\Group;
|
use Friendica\Model\Group;
|
||||||
use Friendica\Network\HTTPException\NotFoundException;
|
use Friendica\Network\HTTPException\NotFoundException;
|
||||||
|
use Friendica\Security\PermissionSet\Exception\PermissionSetNotFoundException;
|
||||||
|
use Friendica\Security\PermissionSet\Exception\PermissionSetPersistenceException;
|
||||||
use Friendica\Security\PermissionSet\Factory;
|
use Friendica\Security\PermissionSet\Factory;
|
||||||
use Friendica\Security\PermissionSet\Collection;
|
use Friendica\Security\PermissionSet\Collection;
|
||||||
use Friendica\Security\PermissionSet\Entity;
|
use Friendica\Security\PermissionSet\Entity;
|
||||||
|
@ -75,12 +77,16 @@ class PermissionSet extends BaseRepository
|
||||||
*
|
*
|
||||||
* @return Entity\PermissionSet
|
* @return Entity\PermissionSet
|
||||||
* @throws NotFoundException
|
* @throws NotFoundException
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function selectOne(array $condition, array $params = []): Entity\PermissionSet
|
private function selectOne(array $condition, array $params = []): Entity\PermissionSet
|
||||||
{
|
{
|
||||||
return parent::_selectOne($condition, $params);
|
return parent::_selectOne($condition, $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
private function select(array $condition, array $params = []): Collection\PermissionSets
|
private function select(array $condition, array $params = []): Collection\PermissionSets
|
||||||
{
|
{
|
||||||
return new Collection\PermissionSets(parent::_select($condition, $params)->getArrayCopy());
|
return new Collection\PermissionSets(parent::_select($condition, $params)->getArrayCopy());
|
||||||
|
@ -108,7 +114,9 @@ class PermissionSet extends BaseRepository
|
||||||
* @param int $id A PermissionSet table row id or self::PUBLIC
|
* @param int $id A PermissionSet table row id or self::PUBLIC
|
||||||
* @param int $uid The owner of the PermissionSet
|
* @param int $uid The owner of the PermissionSet
|
||||||
* @return Entity\PermissionSet
|
* @return Entity\PermissionSet
|
||||||
* @throws NotFoundException
|
*
|
||||||
|
* @throws PermissionSetNotFoundException
|
||||||
|
* @throws PermissionSetPersistenceException
|
||||||
*/
|
*/
|
||||||
public function selectOneById(int $id, int $uid): Entity\PermissionSet
|
public function selectOneById(int $id, int $uid): Entity\PermissionSet
|
||||||
{
|
{
|
||||||
|
@ -116,7 +124,13 @@ class PermissionSet extends BaseRepository
|
||||||
return $this->factory->createFromString($uid);
|
return $this->factory->createFromString($uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return $this->selectOne(['id' => $id, 'uid' => $uid]);
|
return $this->selectOne(['id' => $id, 'uid' => $uid]);
|
||||||
|
} catch (NotFoundException $exception) {
|
||||||
|
throw new PermissionSetNotFoundException(sprintf('PermissionSet with id %d for user %u doesn\'t exist.', $id, $uid), $exception);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
throw new PermissionSetPersistenceException(sprintf('Cannot select PermissionSet %d for user %d', $id, $uid), $exception);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,9 +140,12 @@ class PermissionSet extends BaseRepository
|
||||||
* @param int $uid User id whom the items belong, used for ownership check.
|
* @param int $uid User id whom the items belong, used for ownership check.
|
||||||
*
|
*
|
||||||
* @return Collection\PermissionSets
|
* @return Collection\PermissionSets
|
||||||
|
*
|
||||||
|
* @throws PermissionSetPersistenceException
|
||||||
*/
|
*/
|
||||||
public function selectByContactId(int $cid, int $uid): Collection\PermissionSets
|
public function selectByContactId(int $cid, int $uid): Collection\PermissionSets
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$cdata = Contact::getPublicAndUserContactID($cid, $uid);
|
$cdata = Contact::getPublicAndUserContactID($cid, $uid);
|
||||||
if (!empty($cdata)) {
|
if (!empty($cdata)) {
|
||||||
$public_contact_str = $this->aclFormatter->toString($cdata['public']);
|
$public_contact_str = $this->aclFormatter->toString($cdata['public']);
|
||||||
|
@ -165,6 +182,9 @@ class PermissionSet extends BaseRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->select($condition);
|
return $this->select($condition);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
throw new PermissionSetPersistenceException(sprintf('Cannot select PermissionSet for contact %d and user %d', $cid, $uid), $exception);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -173,11 +193,20 @@ class PermissionSet extends BaseRepository
|
||||||
* @param int $uid
|
* @param int $uid
|
||||||
*
|
*
|
||||||
* @return Entity\PermissionSet
|
* @return Entity\PermissionSet
|
||||||
* @throws Exception
|
*
|
||||||
|
* @throws PermissionSetPersistenceException
|
||||||
*/
|
*/
|
||||||
public function selectDefaultForUser(int $uid): Entity\PermissionSet
|
public function selectDefaultForUser(int $uid): Entity\PermissionSet
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$self_contact = Contact::selectFirst(['id'], ['uid' => $uid, 'self' => true]);
|
$self_contact = Contact::selectFirst(['id'], ['uid' => $uid, 'self' => true]);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
throw new PermissionSetPersistenceException(sprintf('Cannot select Contact for user %d', $uid));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->db->isResult($self_contact)) {
|
||||||
|
throw new PermissionSetPersistenceException(sprintf('No "self" contact found for user %d', $uid));
|
||||||
|
}
|
||||||
|
|
||||||
return $this->selectOrCreate($this->factory->createFromString(
|
return $this->selectOrCreate($this->factory->createFromString(
|
||||||
$uid,
|
$uid,
|
||||||
|
@ -203,6 +232,8 @@ class PermissionSet extends BaseRepository
|
||||||
* @param Entity\PermissionSet $permissionSet
|
* @param Entity\PermissionSet $permissionSet
|
||||||
*
|
*
|
||||||
* @return Entity\PermissionSet
|
* @return Entity\PermissionSet
|
||||||
|
*
|
||||||
|
* @throws PermissionSetPersistenceException
|
||||||
*/
|
*/
|
||||||
public function selectOrCreate(Entity\PermissionSet $permissionSet): Entity\PermissionSet
|
public function selectOrCreate(Entity\PermissionSet $permissionSet): Entity\PermissionSet
|
||||||
{
|
{
|
||||||
|
@ -219,6 +250,8 @@ class PermissionSet extends BaseRepository
|
||||||
return $this->selectOne($this->convertToTableRow($permissionSet));
|
return $this->selectOne($this->convertToTableRow($permissionSet));
|
||||||
} catch (NotFoundException $exception) {
|
} catch (NotFoundException $exception) {
|
||||||
return $this->save($permissionSet);
|
return $this->save($permissionSet);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
throw new PermissionSetPersistenceException(sprintf('Cannot select PermissionSet %d', $permissionSet->id ?? 0), $exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,7 +259,8 @@ class PermissionSet extends BaseRepository
|
||||||
* @param Entity\PermissionSet $permissionSet
|
* @param Entity\PermissionSet $permissionSet
|
||||||
*
|
*
|
||||||
* @return Entity\PermissionSet
|
* @return Entity\PermissionSet
|
||||||
* @throws NotFoundException
|
*
|
||||||
|
* @throws PermissionSetPersistenceException
|
||||||
*/
|
*/
|
||||||
public function save(Entity\PermissionSet $permissionSet): Entity\PermissionSet
|
public function save(Entity\PermissionSet $permissionSet): Entity\PermissionSet
|
||||||
{
|
{
|
||||||
|
@ -237,6 +271,7 @@ class PermissionSet extends BaseRepository
|
||||||
|
|
||||||
$fields = $this->convertToTableRow($permissionSet);
|
$fields = $this->convertToTableRow($permissionSet);
|
||||||
|
|
||||||
|
try {
|
||||||
if ($permissionSet->id) {
|
if ($permissionSet->id) {
|
||||||
$this->db->update(self::$table_name, $fields, ['id' => $permissionSet->id]);
|
$this->db->update(self::$table_name, $fields, ['id' => $permissionSet->id]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -244,6 +279,9 @@ class PermissionSet extends BaseRepository
|
||||||
|
|
||||||
$permissionSet = $this->selectOneById($this->db->lastInsertId(), $permissionSet->uid);
|
$permissionSet = $this->selectOneById($this->db->lastInsertId(), $permissionSet->uid);
|
||||||
}
|
}
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
throw new PermissionSetPersistenceException(sprintf('Cannot save PermissionSet %d', $permissionSet->id ?? 0), $exception);
|
||||||
|
}
|
||||||
|
|
||||||
return $permissionSet;
|
return $permissionSet;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue