summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Cornutt <enygma@phpdeveloper.org>2015-08-05 20:48:35 -0500
committerChris Cornutt <enygma@phpdeveloper.org>2015-08-05 20:48:35 -0500
commitf5a09d8e42aa6754d39bc7932bea413d2520e269 (patch)
treea254694084d7602100e686d4095a42cdbe756953
parent99a27f69d32df6a86805600d8f60a0d9745dec32 (diff)
downloadgatekeeper-f5a09d8e42aa6754d39bc7932bea413d2520e269.zip
gatekeeper-f5a09d8e42aa6754d39bc7932bea413d2520e269.tar.gz
gatekeeper-f5a09d8e42aa6754d39bc7932bea413d2520e269.tar.bz2
Adding the cloning operation (for users first)2.9
-rw-r--r--docs/working-with-objects.md29
-rw-r--r--src/Psecio/Gatekeeper/Gatekeeper.php4
-rw-r--r--src/Psecio/Gatekeeper/Handler/CloneInstance.php47
3 files changed, 79 insertions, 1 deletions
diff --git a/docs/working-with-objects.md b/docs/working-with-objects.md
index 4626820..eeea0d2 100644
--- a/docs/working-with-objects.md
+++ b/docs/working-with-objects.md
@@ -56,3 +56,32 @@ $user = Gatekeeper::findUserByUsername('ccornutt');
$user->delete();
?>
```
+
+## Cloning
+
+You can clone certain kinds of objects in the Gatekeeper system, duplicating the type of object and its relations.
+
+#### Users
+
+You can clone a user with the `Gatekeeper::cloneUser` method. This will create a new user with the data provided and link this new user to the same permissions and groups as the user you're cloning.
+
+```php
+<?php
+$user1 = Gatekeeper::findUserByUsername('ccornutt');
+
+$result = Gatekeeper::cloneUser($user1, [
+ 'username' => 'ccornutt1',
+ 'password' => 'super-secret',
+ 'email' => 'ccornut2@mydomain.com',
+ 'firstName' => 'Chris',
+ 'lastName' => 'Cornutt2'
+]);
+
+if ($result === true) {
+ echo 'User cloned successfully!';
+}
+?>
+```
+
+In this case user `cornutt1` will have the same permissions and groups as `ccornutt`. The result of the `cloneUser` function call is the success/fail status of the creation.
+
diff --git a/src/Psecio/Gatekeeper/Gatekeeper.php b/src/Psecio/Gatekeeper/Gatekeeper.php
index 10c2b84..61bbc53 100644
--- a/src/Psecio/Gatekeeper/Gatekeeper.php
+++ b/src/Psecio/Gatekeeper/Gatekeeper.php
@@ -17,7 +17,7 @@ class Gatekeeper
* @var array
*/
private static $actions = array(
- 'find', 'delete', 'create', 'save'
+ 'find', 'delete', 'create', 'save', 'clone'
);
/**
@@ -462,6 +462,8 @@ class Gatekeeper
$action = new \Psecio\Gatekeeper\Handler\Delete($name, $args, self::$datasource);
} elseif ($action == 'save') {
$action = new \Psecio\Gatekeeper\Handler\Save($name, $args, self::$datasource);
+ } elseif ($action == 'clone') {
+ $action = new \Psecio\Gatekeeper\Handler\CloneInstance($name, $args, self::$datasource);
}
return $action->execute();
}
diff --git a/src/Psecio/Gatekeeper/Handler/CloneInstance.php b/src/Psecio/Gatekeeper/Handler/CloneInstance.php
new file mode 100644
index 0000000..66cc4d3
--- /dev/null
+++ b/src/Psecio/Gatekeeper/Handler/CloneInstance.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Psecio\Gatekeeper\Handler;
+use Psecio\Gatekeeper\Gatekeeper;
+
+class CloneInstance extends \Psecio\Gatekeeper\Handler
+{
+ /**
+ * Execute the object/record clone handling
+ *
+ * @return boolean Success/fail of user cloning
+ */
+ public function execute()
+ {
+ $args = $this->getArguments();
+ $name = $this->getName();
+ $method = ucwords($name);
+
+ if (method_exists($this, $method) === true) {
+ return $this->$method($args[0], $args[1]);
+ }
+ return false;
+ }
+
+ public function CloneUser($user, $data)
+ {
+ $ds = Gatekeeper::getDatasource();
+ $newUser = new \Psecio\Gatekeeper\UserModel($ds, $data);
+ $result = $newUser->save();
+
+ if ($result == false) {
+ return false;
+ }
+
+ // Get the user's groups and add
+ foreach ($user->groups as $group) {
+ $newUser->addGroup($group);
+ }
+
+ // Get the user's permissions and add
+ foreach ($user->permissions as $permission) {
+ $newUser->addPermission($permission);
+ }
+
+ return true;
+ }
+} \ No newline at end of file