Premier commit
This commit is contained in:
248
modules/user/controllers/AdminController.php
Normal file
248
modules/user/controllers/AdminController.php
Normal file
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the Piko user module
|
||||
*
|
||||
* @copyright 2020 Sylvain PHILIP.
|
||||
* @license LGPL-3.0; see LICENSE.txt
|
||||
* @link https://github.com/piko-framework/piko-user
|
||||
*/
|
||||
namespace app\modules\user\controllers;
|
||||
|
||||
use Piko\HttpException;
|
||||
use function Piko\I18n\__;
|
||||
use Piko\User as PikoUser;
|
||||
use app\modules\user\models\Role;
|
||||
use app\modules\user\models\User;
|
||||
use app\modules\user\models\Permission;
|
||||
|
||||
/**
|
||||
* User admin controller
|
||||
*
|
||||
* @author Sylvain PHILIP <contact@sphilip.com>
|
||||
*/
|
||||
class AdminController extends \Piko\Controller
|
||||
{
|
||||
protected PikoUser $user;
|
||||
protected \PDO $db;
|
||||
|
||||
public function init(): void
|
||||
{
|
||||
$app = $this->module->getApplication();
|
||||
|
||||
$user = $app->getComponent('Piko\User');
|
||||
assert($user instanceof PikoUser);
|
||||
$this->user = $user;
|
||||
|
||||
$db = $app->getComponent('PDO');
|
||||
assert($db instanceof \PDO);
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see \piko\Controller::runAction()
|
||||
*/
|
||||
public function runAction($id)
|
||||
{
|
||||
assert($this->module instanceof \app\modules\user\Module);
|
||||
|
||||
if (!$this->user->can($this->module->adminRole)) {
|
||||
throw new HttpException('Not authorized.', 403);
|
||||
}
|
||||
|
||||
return parent::runAction($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render users view
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function usersAction()
|
||||
{
|
||||
return $this->render('users', [
|
||||
'users' => User::find()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render User form and create or update user
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function editAction(int $id = 0)
|
||||
{
|
||||
$user = new User($this->db);
|
||||
|
||||
if ($id) {
|
||||
$user->load($id);
|
||||
}
|
||||
|
||||
$user->scenario = User::SCENARIO_ADMIN;
|
||||
$message = false;
|
||||
|
||||
$post = $this->request->getParsedBody();
|
||||
|
||||
if (!empty($post)) {
|
||||
|
||||
$user->bind($post);
|
||||
|
||||
if ($user->isValid() && $user->save()) {
|
||||
$message['type'] = 'success';
|
||||
$message['content'] = __('user', 'User successfully saved');
|
||||
} else {
|
||||
$message['type'] = 'danger';
|
||||
$message['content'] = __('user', 'Save error!') . implode(' ', $user->errors);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('edit', [
|
||||
'user' => $user,
|
||||
'message' => $message,
|
||||
'roles' => Role::find('`name` ASC'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete users
|
||||
*/
|
||||
public function deleteAction()
|
||||
{
|
||||
$post = $this->request->getParsedBody();
|
||||
$ids = isset($post['items'])? $post['items'] : [];
|
||||
|
||||
foreach ($ids as $id) {
|
||||
$user = new User($id);
|
||||
$user->delete();
|
||||
}
|
||||
|
||||
$this->redirect($this->getUrl('user/admin/users'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Render roles view
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function rolesAction()
|
||||
{
|
||||
return $this->render('roles', [
|
||||
'roles' => Role::find(),
|
||||
'permissions' => Permission::find('`name` ASC'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create/update role (AJAX)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function editRoleAction(int $id = 0)
|
||||
{
|
||||
$role = new Role($this->db);
|
||||
|
||||
if ($id) {
|
||||
$role->load($id);
|
||||
}
|
||||
|
||||
$role->scenario = Role::SCENARIO_ADMIN;
|
||||
|
||||
$post = $this->request->getParsedBody();
|
||||
|
||||
$response = [
|
||||
'role' => $role
|
||||
];
|
||||
|
||||
if (!empty($post)) {
|
||||
|
||||
$role->bind($post);
|
||||
|
||||
if ($role->isValid() && $role->save()) {
|
||||
$response['status'] = 'success';
|
||||
} else {
|
||||
$response['status'] = 'error';
|
||||
}
|
||||
}
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete roles
|
||||
*/
|
||||
public function deleteRolesAction()
|
||||
{
|
||||
$post = $this->request->getParsedBody();
|
||||
$ids = isset($post['items'])? $post['items'] : [];
|
||||
|
||||
foreach ($ids as $id) {
|
||||
$item = new Role($id);
|
||||
$item->delete();
|
||||
}
|
||||
|
||||
$this->redirect($this->getUrl('user/admin/roles'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Render permissions view
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function permissionsAction()
|
||||
{
|
||||
return $this->render('permissions', [
|
||||
'permissions' => Permission::find()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create/update permission (AJAX)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function editPermissionAction(int $id = 0)
|
||||
{
|
||||
$permission = new Permission($this->db);
|
||||
|
||||
if ($id) {
|
||||
$permission->load($id);
|
||||
}
|
||||
|
||||
$response = [
|
||||
'permission' => $permission
|
||||
];
|
||||
|
||||
$post = $this->request->getParsedBody();
|
||||
|
||||
if (!empty($post)) {
|
||||
|
||||
$permission->bind($post);
|
||||
|
||||
if ($permission->isValid() && $permission->save()) {
|
||||
$response['status'] = 'success';
|
||||
} else {
|
||||
$response['status'] = 'error';
|
||||
$response['error'] = array_pop($permission->getErrors());
|
||||
}
|
||||
}
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete permissions
|
||||
*/
|
||||
public function deletePermissionsAction()
|
||||
{
|
||||
$post = $this->request->getParsedBody();
|
||||
$ids = isset($post['items'])? $post['items'] : [];
|
||||
|
||||
foreach ($ids as $id) {
|
||||
$item = new Permission($id);
|
||||
$item->delete();
|
||||
}
|
||||
|
||||
$this->redirect($this->getUrl('user/admin/permissions'));
|
||||
}
|
||||
}
|
||||
295
modules/user/controllers/DefaultController.php
Normal file
295
modules/user/controllers/DefaultController.php
Normal file
@@ -0,0 +1,295 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the Piko user module
|
||||
*
|
||||
* @copyright 2020 Sylvain PHILIP.
|
||||
* @license LGPL-3.0; see LICENSE.txt
|
||||
* @link https://github.com/piko-framework/piko-user
|
||||
*/
|
||||
namespace app\modules\user\controllers;
|
||||
|
||||
use function Piko\I18n\__;
|
||||
use piko\HttpException;
|
||||
use app\modules\user\models\User;
|
||||
use Piko\User as PikoUser;
|
||||
|
||||
/**
|
||||
* User default controller
|
||||
*
|
||||
* @author Sylvain PHILIP <contact@sphilip.com>
|
||||
*/
|
||||
class DefaultController extends \Piko\Controller
|
||||
{
|
||||
protected PikoUser $user;
|
||||
protected \PDO $db;
|
||||
|
||||
public function init(): void
|
||||
{
|
||||
$app = $this->module->getApplication();
|
||||
|
||||
$user = $app->getComponent('Piko\User');
|
||||
assert($user instanceof PikoUser);
|
||||
$this->user = $user;
|
||||
|
||||
$db = $app->getComponent('PDO');
|
||||
assert($db instanceof \PDO);
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render and process user registration
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function registerAction()
|
||||
{
|
||||
if (!$this->user->isGuest()) {
|
||||
return $this->redirect('/');
|
||||
}
|
||||
|
||||
$message = false;
|
||||
$post = $this->request->getParsedBody();
|
||||
|
||||
if (!empty($post)) {
|
||||
|
||||
$user = new User($this->db);
|
||||
|
||||
$user->scenario = User::SCENARIO_REGISTER;
|
||||
|
||||
$user->bind($post);
|
||||
|
||||
if ($user->isValid() && $user->save()) {
|
||||
// $user->sendRegistrationConfirmation();
|
||||
$message['type'] = 'success';
|
||||
$message['content'] = __(
|
||||
'user',
|
||||
'Your account was created. Please activate it through the confirmation email that was sent to you.'
|
||||
);
|
||||
} else {
|
||||
$message['type'] = 'danger';
|
||||
$message['content'] = implode(', ', $user->errors);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('register', [
|
||||
'message' => $message,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate registration (AJAX)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function checkRegistrationAction()
|
||||
{
|
||||
$errors = [];
|
||||
$this->layout = false;
|
||||
$post = $this->request->getParsedBody();
|
||||
|
||||
if (!empty($post)) {
|
||||
|
||||
$user = new User($this->db);
|
||||
$user->scenario = 'register';
|
||||
$user->bind($post);
|
||||
$user->isValid();
|
||||
$errors = $user->getErrors();
|
||||
}
|
||||
|
||||
return $this->jsonResponse($errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render user activation confirmation
|
||||
*
|
||||
* @throws HttpException
|
||||
* @return string
|
||||
*/
|
||||
public function confirmationAction($token)
|
||||
{
|
||||
$user = User::findByAuthKey($token);
|
||||
|
||||
if (!$user) {
|
||||
throw new HttpException('Not found.', 404);
|
||||
}
|
||||
|
||||
$message = false;
|
||||
|
||||
if (!$user->isActivated()) {
|
||||
|
||||
if ($user->activate()) {
|
||||
$message['type'] = 'success';
|
||||
$message['content'] = __('user', 'Your account has been activated. You can now log in.');
|
||||
} else {
|
||||
$message['type'] = 'danger';
|
||||
$message['content'] = __(
|
||||
'user',
|
||||
'Unable to activate your account. Please contact the site manager.'
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$message['type'] = 'warning';
|
||||
$message['content'] = __('user', 'Your account has already been activated.');
|
||||
}
|
||||
|
||||
return $this->render('login', ['message' => $message]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render reminder password form and send email to change password
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function reminderAction()
|
||||
{
|
||||
$message = false;
|
||||
$post = $this->request->getParsedBody();
|
||||
|
||||
$reminder = $post['reminder']?? '';
|
||||
|
||||
if (!empty($reminder)) {
|
||||
|
||||
$user = User::findByUsername($reminder);
|
||||
|
||||
if (!$user) {
|
||||
$user = User::findByEmail($reminder);
|
||||
}
|
||||
|
||||
if ($user) {
|
||||
// $user->sendResetPassword();
|
||||
$message['type'] = 'success';
|
||||
$message['content'] = __(
|
||||
'user',
|
||||
'A link has been sent to you by email ({email}). It will allow you to recreate your password.',
|
||||
['email' => $user->email]
|
||||
);
|
||||
} else {
|
||||
$message['type'] = 'danger';
|
||||
$message['content'] = __('user', 'Account not found.');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('reminder', [
|
||||
'message' => $message,
|
||||
'reminder' => $reminder,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render and process reset password
|
||||
*
|
||||
* @throws HttpException
|
||||
* @return string
|
||||
*/
|
||||
public function resetPasswordAction($token)
|
||||
{
|
||||
$user = User::findByAuthKey($token);
|
||||
|
||||
if (!$user) {
|
||||
throw new HttpException('Not found', 404);
|
||||
}
|
||||
|
||||
$message = false;
|
||||
$post = $this->request->getParsedBody();
|
||||
|
||||
if (!empty($post)) {
|
||||
$user->scenario = 'reset';
|
||||
|
||||
$user->bind($post);
|
||||
|
||||
if ($user->isValid() && $user->save()) {
|
||||
$message['type'] = 'success';
|
||||
$message['content'] = __('user', 'Your password has been successfully updated.');
|
||||
} else {
|
||||
$message['type'] = 'danger';
|
||||
$message['content'] = implode(', ', $user->errors);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('reset', [
|
||||
'message' => $message,
|
||||
'user' => $user,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render user form and update changes
|
||||
*
|
||||
* @throws HttpException
|
||||
* @return string
|
||||
*/
|
||||
public function editAction()
|
||||
{
|
||||
if ($this->user->isGuest()) {
|
||||
throw new HttpException(__('user', 'You must be logged to access this page.'), 401);
|
||||
}
|
||||
|
||||
$identity = $this->user->getIdentity();
|
||||
|
||||
assert($identity instanceof User);
|
||||
|
||||
$message = false;
|
||||
$post = $this->request->getParsedBody();
|
||||
|
||||
if (!empty($post)) {
|
||||
$identity->bind($post);
|
||||
|
||||
if ($identity->isValid() && $identity->save()) {
|
||||
$message['type'] = 'success';
|
||||
$message['content'] = __('user', 'Changes saved!');
|
||||
} else {
|
||||
$message['type'] = 'danger';
|
||||
$message['content'] = implode(', ', $identity->getErrors());
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('edit', [
|
||||
'user' => $identity,
|
||||
'message' => $message,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render login form and process login
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function loginAction()
|
||||
{
|
||||
$message = false;
|
||||
$post = $this->request->getParsedBody();
|
||||
|
||||
if (!empty($post)) {
|
||||
$identity = User::findByUsername($post['username']);
|
||||
|
||||
if ($identity instanceof User && $identity->validatePassword($post['password'])) {
|
||||
|
||||
$this->user->login($identity);
|
||||
$identity->last_login_at = time();
|
||||
$identity->save();
|
||||
|
||||
return $this->redirect('/');
|
||||
|
||||
} else {
|
||||
$message['type'] = 'danger';
|
||||
$message['content'] = __('user', 'Authentication failure');
|
||||
}
|
||||
}
|
||||
|
||||
assert($this->module instanceof \app\modules\user\Module);
|
||||
|
||||
return $this->render('login', [
|
||||
'message' => $message,
|
||||
'canRegister' => $this->module->allowUserRegistration
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* User logout
|
||||
*/
|
||||
public function logoutAction()
|
||||
{
|
||||
$this->user->logout();
|
||||
$this->redirect('/');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user