From 3266f220b9479b1a31c163ea511ca5b4e2b8247a Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 14 Feb 2016 21:49:00 -0500 Subject: [PATCH] should close #42; references #57 --- app/Repositories/SubuserRepository.php | 133 +++++++++++++------------ 1 file changed, 67 insertions(+), 66 deletions(-) diff --git a/app/Repositories/SubuserRepository.php b/app/Repositories/SubuserRepository.php index 556ab7b0..459a70a9 100644 --- a/app/Repositories/SubuserRepository.php +++ b/app/Repositories/SubuserRepository.php @@ -114,50 +114,50 @@ class SubuserRepository DB::beginTransaction(); - // Determine if this user exists or if we need to make them an account. - $user = Models\User::where('email', $data['email'])->first(); - if (!$user) { - $password = str_random(16); - try { - $repo = new UserRepository; - $uid = $repo->create($data['email'], $password); - $user = Models\User::findOrFail($uid); - } catch (\Exception $ex) { - throw $ex; - } - } - - $uuid = new UuidService; - - $subuser = new Models\Subuser; - $subuser->fill([ - 'user_id' => $user->id, - 'server_id' => $server->id, - 'daemonSecret' => (string) $uuid->generate('servers', 'uuid') - ]); - $subuser->save(); - - $daemonPermissions = $this->coreDaemonPermissions; - foreach($data['permissions'] as $permission) { - if (array_key_exists($permission, $this->permissions)) { - // Build the daemon permissions array for sending. - if (!is_null($this->permissions[$permission])) { - array_push($daemonPermissions, $this->permissions[$permission]); - } - $model = new Models\Permission; - $model->fill([ - 'user_id' => $user->id, - 'server_id' => $server->id, - 'permission' => $permission - ]); - $model->save(); - } - } - - // Contact Daemon - // We contact even if they don't have any daemon permissions to overwrite - // if they did have them previously. try { + // Determine if this user exists or if we need to make them an account. + $user = Models\User::where('email', $data['email'])->first(); + if (!$user) { + $password = str_random(16); + try { + $repo = new UserRepository; + $uid = $repo->create($data['email'], $password); + $user = Models\User::findOrFail($uid); + } catch (\Exception $ex) { + throw $ex; + } + } + + $uuid = new UuidService; + + $subuser = new Models\Subuser; + $subuser->fill([ + 'user_id' => $user->id, + 'server_id' => $server->id, + 'daemonSecret' => (string) $uuid->generate('servers', 'uuid') + ]); + $subuser->save(); + + $daemonPermissions = $this->coreDaemonPermissions; + foreach($data['permissions'] as $permission) { + if (array_key_exists($permission, $this->permissions)) { + // Build the daemon permissions array for sending. + if (!is_null($this->permissions[$permission])) { + array_push($daemonPermissions, $this->permissions[$permission]); + } + $model = new Models\Permission; + $model->fill([ + 'user_id' => $user->id, + 'server_id' => $server->id, + 'permission' => $permission + ]); + $model->save(); + } + } + + // Contact Daemon + // We contact even if they don't have any daemon permissions to overwrite + // if they did have them previously. $node = Models\Node::getByID($server->node); $client = Models\Node::guzzleRequest($server->node); @@ -210,9 +210,9 @@ class SubuserRepository DB::beginTransaction(); - Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); - try { + Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); + $node = Models\Node::getByID($server->node); $client = Models\Node::guzzleRequest($server->node); @@ -265,29 +265,30 @@ class SubuserRepository $server = Models\Server::findOrFail($data['server']); DB::beginTransaction(); - Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); - $daemonPermissions = $this->coreDaemonPermissions; - foreach($data['permissions'] as $permission) { - if (array_key_exists($permission, $this->permissions)) { - // Build the daemon permissions array for sending. - if (!is_null($this->permissions[$permission])) { - array_push($daemonPermissions, $this->permissions[$permission]); - } - $model = new Models\Permission; - $model->fill([ - 'user_id' => $data['user'], - 'server_id' => $data['server'], - 'permission' => $permission - ]); - $model->save(); - } - } - - // Contact Daemon - // We contact even if they don't have any daemon permissions to overwrite - // if they did have them previously. try { + Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); + + $daemonPermissions = $this->coreDaemonPermissions; + foreach($data['permissions'] as $permission) { + if (array_key_exists($permission, $this->permissions)) { + // Build the daemon permissions array for sending. + if (!is_null($this->permissions[$permission])) { + array_push($daemonPermissions, $this->permissions[$permission]); + } + $model = new Models\Permission; + $model->fill([ + 'user_id' => $data['user'], + 'server_id' => $data['server'], + 'permission' => $permission + ]); + $model->save(); + } + } + + // Contact Daemon + // We contact even if they don't have any daemon permissions to overwrite + // if they did have them previously. $node = Models\Node::getByID($server->node); $client = Models\Node::guzzleRequest($server->node);