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; }
public function addUser(array $options, $output) { $user = Gatekeeper::findUserById($options['userid']); $ds = Gatekeeper::getDatasource(); if (isset($options['permission'])) { // If it's a permission link it to the user $perm = new \Psecio\Gatekeeper\UserPermissionModel($ds, array('userId' => $user->id, 'permissionId' => $options['permission'])); if ($ds->save($perm) === true) { $output->writeln('Permission linked to user successfully'); } } elseif (isset($options['group'])) { // If it's a group link it to the user $group = new \Psecio\Gatekeeper\UserGroupModel($ds, array('userId' => $user->id, 'groupId' => $options['group'])); if ($ds->save($group) === true) { $output->writeln('Group linked to user successfully'); } } }
public function processSignupAction() { try { v::email()->check($_POST['email']); v::length(6)->check($_POST['password']); } catch (ValidationException $e) { $this->flasher->error('Please make sure your password is longer than 6 characters, and that your username is a valid email address!'); } if ($_POST['password'] !== $_POST['password_confirm']) { $this->flasher->error('Passwords need to be identical'); } if ($this->flasher->hasMessages('error')) { $this->redirect('/auth'); } $this->initGroups(); // Create an account if none exists $user = Gatekeeper::register(['first_name' => '-', 'last_name' => '-', 'username' => $_POST['email'], 'email' => $_POST['email'], 'password' => $_POST['password'], 'groups' => Gatekeeper::countUser() ? ['users'] : ['admin', 'users']]); if ($user) { $this->flasher->success('Account successfully registered! Please log in!'); } else { $this->flasher->error('Error #GK01: Account creation failed!' . Gatekeeper::getDatasource()->getLastError()); } $this->redirect('/auth'); }
} $data['user'] = $user->toArray(); echo $view->render('users/edit.php', $data); } catch (\Exception $e) { if (ACCEPT_JSON) { $app->response->setStatus(404); } $data = array('message' => $e->getMessage()); echo $view->render('error/index.php', $data); } }); $app->get('/delete/:userId', function ($userId) use($app, $view) { $data = array(); try { $user = g::findUserById($userId); $ds = g::getDatasource(); if ($ds->delete($user) === false) { throw new \Exception('Error deleting user.'); } echo $view->render('users/delete.php', $data); } catch (\Exception $e) { if (ACCEPT_JSON) { $app->response->setStatus(404); } $data = array('message' => $e->getMessage()); echo $view->render('error/index.php', $data); } }); $app->get('/status/:userId', function ($userId) use($app, $view) { $user = g::findUserById($userId); $user->status === 'active' ? $user->deactivate() : $user->activate();
/** * Show the permissions for a user * * @param array $options Command line options * @param OutputInterface $output Output interface object */ public function showUserGroups(array $options = array(), $output) { if (empty($options['id'])) { throw new \InvalidArgumentException('You must specify a user ID!'); } $user = Gatekeeper::findUserById($options['id']); $output->writeln("\n" . 'Showing groups for <options=bold>' . $user->username . '</options=bold>'); $params = array('userId' => $options['id']); $columns = array('name' => 'Name', 'description' => 'Description', 'created' => 'Date Created', 'updated' => 'Date Updated', 'id' => 'ID'); $data = array(); $ds = Gatekeeper::getDatasource(); $groups = Gatekeeper::findUserGroups($params); foreach ($groups->toArray(true) as $group) { $groupModel = new \Psecio\Gatekeeper\GroupModel($ds); $groupModel = $ds->find($groupModel, array('id' => $group['groupId'])); $data[] = $groupModel->toArray(); } $this->buildTable($columns, $data, $output); }
/** * Set the "remember me" token value * * @param string $value Token value */ public function setRememberToken($value) { $tokens = $this->model->authTokens; if (isset($tokens[0])) { $token = $tokens[0]; $token->token($value); $token->save(); } else { // No token found, make one $token = new AuthTokenModel(Gatekeeper::getDatasource(), ['token' => $value, 'user_id' => $this->model->id, 'expires' => strtotime('+14 days')]); $token->save(); } }
/** * Save the current model instance (gets datasource and calls save) * * @return boolean Success/fail result of save */ public function save() { $ds = \Psecio\Gatekeeper\Gatekeeper::getDatasource(); return $ds->save($this); }