/** * {@inheritdoc} */ public function main(App $app) { $app['user'] = function ($app) { if (!($user = $app['auth']->getUser())) { $user = User::create(['roles' => [Role::ROLE_ANONYMOUS]]); } return $user; }; }
/** * @Access("user: manage users") * @Request({"id": "int"}) */ public function editAction($id = 0) { if (!$id) { $user = User::create(['roles' => [Role::ROLE_AUTHENTICATED]]); } else { if (!($user = User::find($id))) { App::abort(404, 'User not found.'); } } return ['$view' => ['title' => $id ? __('Edit User') : __('Add User'), 'name' => 'system/user/admin/user-edit.php'], '$data' => ['user' => $user, 'config' => ['statuses' => User::getStatuses(), 'roles' => array_values($this->getRoles($user)), 'emailVerification' => App::module('system/user')->config('require_verification'), 'currentUser' => App::user()->id]]]; }
/** * @Request({"user": "******"}) */ public function registerAction($data) { $message = ''; try { if (App::user()->isAuthenticated() || $this->module->config('registration') == 'admin') { return App::redirect(); } if (!App::csrf()->validate()) { throw new Exception(__('Invalid token. Please try again.')); } $password = @$data['password']; if (trim($password) != $password || strlen($password) < 6) { throw new Exception(__('Password must be 6 characters or longer.')); } $user = User::create(['registered' => new \DateTime(), 'name' => @$data['name'], 'username' => @$data['username'], 'email' => @$data['email'], 'password' => App::get('auth.password')->hash($password), 'status' => User::STATUS_BLOCKED]); $token = App::get('auth.random')->generateString(32); $admin = $this->module->config('registration') == 'approval'; if ($verify = $this->module->config('require_verification')) { $user->activation = $token; } elseif ($admin) { $user->activation = $token; $user->set('verified', true); } else { $user->status = User::STATUS_ACTIVE; } $user->validate(); $user->save(); if ($verify) { $this->sendVerificationMail($user); $message = __('Complete your registration by clicking the link provided in the mail that has been sent to you.'); } elseif ($admin) { $this->sendApproveMail($user); $message = __('Your user account has been created and is pending approval by the site administrator.'); } else { $this->sendWelcomeEmail($user); $message = __('Your user account has been created.'); } } catch (Exception $e) { App::abort(400, $e->getMessage()); } App::message()->success($message); return ['message' => $message, 'redirect' => App::url('@user/login', [], true)]; }
/** * @Route("/", methods="POST") * @Route("/{id}", methods="POST", requirements={"id"="\d+"}) * @Request({"user": "******", "password", "id": "int"}, csrf=true) */ public function saveAction($data, $password = null, $id = 0) { try { // is new ? if (!($user = User::find($id))) { if ($id) { App::abort(404, __('User not found.')); } if (!$password) { App::abort(400, __('Password required.')); } $user = User::create(['registered' => new \DateTime()]); } $user->name = @$data['name']; $user->username = @$data['username']; $user->email = @$data['email']; $self = App::user()->id == $user->id; if ($self && @$data['status'] == User::STATUS_BLOCKED) { App::abort(400, __('Unable to block yourself.')); } if (@$data['email'] != $user->email) { $user->set('verified', false); } if (!empty($password)) { if (trim($password) != $password || strlen($password) < 3) { throw new Exception(__('Invalid Password.')); } $user->password = App::get('auth.password')->hash($password); } $key = array_search(Role::ROLE_ADMINISTRATOR, @$data['roles'] ?: []); $add = false !== $key && !$user->isAdministrator(); $remove = false === $key && $user->isAdministrator(); if ($self && $remove || !App::user()->isAdministrator() && ($remove || $add)) { App::abort(403, 'Cannot add/remove Admin Role.'); } unset($data['access'], $data['login'], $data['registered']); $user->validate(); $user->save($data); return ['message' => 'success', 'user' => $user]; } catch (Exception $e) { App::abort(400, $e->getMessage()); } }
}, 'view.scripts' => function ($event, $scripts) use($app) { $version = $app->module('bixie/pk-framework')->getVersionKey($app->package('bixie/userprofile')->get('version')); $scripts->register('link-userprofile', 'bixie/userprofile:app/bundle/link-userprofile.js', '~panel-link', ['version' => $version]); $scripts->register('user-section-userprofile', 'bixie/userprofile:app/bundle/user-section-userprofile.js', ['~user-edit', 'bixie-fieldtypes'], ['version' => $version]); }, 'view.data' => function ($event, $data) use($app) { $route = $app->request()->attributes->get('_route'); if (strpos($route, '@userprofile') === 0 || $route == '@user/edit') { $data->add('$fieldtypes', ['ajax_url' => 'api/userprofile/profile/ajax']); } //load profile if (in_array($route, ['@userprofile', '@userprofile/registration', '@user/edit'])) { $self = $app->user(); $edit_id = $app->request()->get('id'); if ($route == '@user/edit') { //blank user when admin creates new user $user = $edit_id ? \Pagekit\User\Model\User::find($edit_id) : \Pagekit\User\Model\User::create(); } else { $user = $self; } if ($self->hasAccess('user: manage users') || $user->id == $self->id) { $profileUser = ProfileUser::load($user); $data->add('$userprofile', ['fields' => array_values(\Bixie\Userprofile\Model\Field::getProfileFields()), 'profilevalues' => $app->module('bixie/userprofile')->getProfile($user), 'profile_user' => $profileUser]); } } }, 'view.styles' => function ($event, $styles) use($app) { $route = $app->request()->attributes->get('_route'); if (strpos($route, '@userprofile') === 0 || in_array($route, ['@user/edit'])) { foreach ($app->module('bixie/userprofile')->getFieldTypes() as $type) { $type->addStyles($styles); } }