Page MenuHomeDevCentral

No OneTemporary

diff --git a/workspaces/src/Engines/Auth/Actions/AddToGroupUserAction.php b/workspaces/src/Engines/Auth/Actions/AddToGroupUserAction.php
index 6955065..e2dbdb9 100644
--- a/workspaces/src/Engines/Auth/Actions/AddToGroupUserAction.php
+++ b/workspaces/src/Engines/Auth/Actions/AddToGroupUserAction.php
@@ -1,76 +1,84 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Add to group user action class
*
* @package ObsidianWorkspaces
* @subpackage Auth
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*
*/
namespace Waystone\Workspaces\Engines\Auth\Actions;
use Waystone\Workspaces\Engines\Auth\UserAction;
use Waystone\Workspaces\Engines\Serialization\ArrayDeserializable;
use Exception;
+use JsonSerializable;
use UserGroup;
/**
* User action to add a user into a group
*/
-class AddToGroupUserAction extends UserAction implements ArrayDeserializable {
+class AddToGroupUserAction extends UserAction implements ArrayDeserializable, JsonSerializable {
/**
* @var UserGroup The group to add the user to
*/
public $group;
/**
* @var boolean Determines if the target user has to be added to the group
* in the quality of admin
*/
public $isAdmin;
/**
* Executes the user action
*/
public function run () {
if ($this->targetUser->isMemberOfGroup($this->group)) {
if ($this->isAdmin) {
//Promotes to admin if needed
$this->targetUser->addToGroup($this->group, true);
}
} else {
//Adds user to the group
$this->targetUser->addToGroup($this->group, $this->isAdmin);
}
}
/**
* Loads an AddToGroupUserAction instance from an object.
*
* @param array $data The associative array to deserialize
*
* @return AddToGroupUserAction The deserialized instance
* @throws Exception when the group code is not found
*/
public static function loadFromArray (array $data) : self {
$instance = new AddToGroupUserAction();
$instance->group = UserGroup::fromCode($data["code"]);
$instance->isAdmin = ($data["isAdmin"] == true);
return $instance;
}
+
+ public function jsonSerialize () : array {
+ return [
+ "code" => $this->group->code,
+ "isAdmin" => $this->isAdmin
+ ];
+ }
}
diff --git a/workspaces/src/Engines/Auth/Actions/GivePermissionUserAction.php b/workspaces/src/Engines/Auth/Actions/GivePermissionUserAction.php
index a174789..6c5e1cb 100644
--- a/workspaces/src/Engines/Auth/Actions/GivePermissionUserAction.php
+++ b/workspaces/src/Engines/Auth/Actions/GivePermissionUserAction.php
@@ -1,134 +1,139 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Give permission user action class
*
* @package ObsidianWorkspaces
* @subpackage Auth
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*
*/
namespace Waystone\Workspaces\Engines\Auth\Actions;
use Waystone\Workspaces\Engines\Auth\Permission;
use Waystone\Workspaces\Engines\Auth\UserAction;
use Waystone\Workspaces\Engines\Framework\Resources;
use Waystone\Workspaces\Engines\Serialization\ArrayDeserializable;
use Exception;
use InvalidArgumentException;
use JsonSerializable;
/**
* User action to grant user a permission
*/
class GivePermissionUserAction extends UserAction
implements ArrayDeserializable, JsonSerializable {
/**
* @var string The permission name
*/
public $permissionName;
/**
* @var int The permission flag
*/
public $permissionFlag = 1;
/**
* @var string The target resource type
*/
public $resourceType;
/**
* @var string The target resource identifier
*/
public $resourceIdentifier;
/**
* Executes the user action
*/
public function run () {
$id = Resources::resolveID($this->resourceType, $this->resourceIdentifier);
if ($id->isNone()) {
throw new Exception("Can't resolve resource "
. $this->resourceType . " " . $this->resourceIdentifier);
}
$this->targetUser->setPermission(
$this->resourceType, $id->getValue(),
$this->permissionName, $this->permissionFlag,
);
}
/**
* Loads a GivePermissionUserAction instance from an associative array.
*
* @param object $data The associative array to deserialize
*
* @return GivePermissionUserAction The deserialized instance
*/
public static function loadFromArray (mixed $data) : self {
// Validate mandatory data
if (!array_key_exists("resource", $data)) {
throw new InvalidArgumentException("A resource property, with two mandatory type and id property is required.");
}
if (!array_key_exists("permission", $data)) {
throw new InvalidArgumentException("A permission property, with a mandatory name property and a facultative flag property is required.");
}
$resource = $data["resource"];
$permission = $data["permission"];
if (!array_key_exists("name", $permission)) {
throw new InvalidArgumentException("Permission name is required.");
}
if (!array_key_exists("type", $resource)) {
throw new InvalidArgumentException("Resource type is required.");
}
if (!array_key_exists("id", $resource)) {
throw new InvalidArgumentException("Resource id is required.");
}
// Build instance
$instance = new GivePermissionUserAction();
$instance->resourceType =
Permission::getResourceTypeLetterFromCode($resource["type"]);
$instance->resourceIdentifier = $resource["id"];
$instance->permissionName = $permission["name"];
if (array_key_exists("flag", $permission)) {
$instance->permissionFlag = $permission["flag"];
}
return $instance;
}
/**
* Serializes the object to a value that can be serialized natively by
* json_encode().
*
- * @return object The serializable value
+ * @return array The serializable value
*/
- public function jsonSerialize () {
- //TODO: if you wish strict code here, we need such a class.
- $data->resource->type =
- Permission::getResourceTypeCodeFromLetter($this->resourceType);
- $data->resource->id = $this->resourceIdentifier;
- $data->permission->name = $this->permissionName;
- $data->permission->flag = $this->permissionFlag;
-
- return $data;
+ public function jsonSerialize () : array {
+ $type = Permission::getResourceTypeCodeFromLetter($this->resourceType);
+
+ return [
+ "resource" => [
+ "type" => $type,
+ "id" => $this->resourceIdentifier
+ ],
+
+ "permission" => [
+ "name" => $this->permissionName,
+ "flag" => $this->permissionFlag,
+ ],
+ ];
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sat, Mar 7, 02:54 (20 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3500455
Default Alt Text
(7 KB)

Event Timeline