add support status on users

This commit is contained in:
osaajani 2020-03-30 01:52:53 +02:00
parent e59631607b
commit 413c058ffc
10 changed files with 156 additions and 8 deletions

View File

@ -85,9 +85,11 @@ namespace adapters;
*/ */
public static function meta_description(): string public static function meta_description(): string
{ {
$callback = \descartes\Router::url('Callback', 'update_sended_status', ['adapter_name' => self::meta_name()], ['api_key' => $_SESSION['user']['api_key'] ?? '<your_api_key>']); $callback = \descartes\Router::url('Callback', 'update_sended_status', ['adapter_name' => self::meta_name()], ['api_key' => $_SESSION['user']['api_key'] ?? '<your_api_key>']);
$generate_credentials_url = 'https://eu.api.ovh.com/createToken/index.cgi?GET=/sms&GET=/sms/*&POST=/sms/*&PUT=/sms/*&DELETE=/sms/*&';
return ' return '
Solution de SMS proposé par le groupe <a target="_blank" href="https://www.ovhtelecom.fr/sms/">OVH</a>. Pour générer les clefs API OVH, <a target="_blank" href="https://api.ovh.com/createToken/index.cgi">cliquez ici.</a> Solution de SMS proposé par le groupe <a target="_blank" href="https://www.ovhtelecom.fr/sms/">OVH</a>. Pour générer les clefs API OVH, <a target="_blank" href="' . $generate_credentials_url . '">cliquez ici.</a>
<br/> <br/>
<br/> <br/>
<div class="alert alert-info">Adresse URL de callback de changement d\'état : <b>' . $callback . '</b></div> <div class="alert alert-info">Adresse URL de callback de changement d\'état : <b>' . $callback . '</b></div>

View File

@ -66,8 +66,9 @@ namespace controllers\internals;
* @param $password : User password * @param $password : User password
* @param $admin : Is user admin * @param $admin : Is user admin
* @param $api_key : User API key, if null random api key is generated * @param $api_key : User API key, if null random api key is generated
* @param $status : User status, default \models\User::STATUS_ACTIVE
*/ */
public function create_update_user(string $email, string $password, bool $admin, ?string $api_key = null) public function create_update_user(string $email, string $password, bool $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE)
{ {
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8'); $bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
$internal_user = new \controllers\internals\User($bdd); $internal_user = new \controllers\internals\User($bdd);
@ -76,12 +77,34 @@ namespace controllers\internals;
if ($user) if ($user)
{ {
$api_key = $api_key ?? $internal_user->generate_random_api_key(); $api_key = $api_key ?? $internal_user->generate_random_api_key();
$success = $internal_user->update($user['id'], $email, $password, $admin, $api_key); $success = $internal_user->update($user['id'], $email, $password, $admin, $api_key, $status);
exit($success ? 0 : 1); exit($success ? 0 : 1);
} }
$success = $internal_user->create($email, $password, $admin, $api_key); $success = $internal_user->create($email, $password, $admin, $api_key, $status);
exit($success ? 0 : 1);
}
/**
*
* Update a user status
*
* @param string $email : User email
* @param string $status : User status, default \models\User::STATUS_ACTIVE
*/
public function update_user_status (string $email, string $status)
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
$internal_user = new \controllers\internals\User($bdd);
$user = $internal_user->get_by_email($email);
if (!$user)
{
exit(1);
}
$success = $internal_user->update_status($user['id'], $status);
exit($success ? 0 : 1); exit($success ? 0 : 1);
} }
} }

View File

@ -104,6 +104,20 @@ namespace controllers\internals;
{ {
return (bool) $this->model_user->update_email($id, $email); return (bool) $this->model_user->update_email($id, $email);
} }
/**
* Update user status.
*
* @param string $id : user id
* @param string $status : new status
*
* @return boolean;
*/
public function update_status($id, $status)
{
return (bool) $this->model_user->update($id, ['status' => $status]);
}
/** /**
* Update user api key. * Update user api key.
@ -170,16 +184,18 @@ namespace controllers\internals;
* @param mixed $password * @param mixed $password
* @param mixed $admin * @param mixed $admin
* @param mixed $api_key * @param mixed $api_key
* @param string $status : User status
* *
* @return int : Number of modified user * @return int : Number of modified user
*/ */
public function update($id, $email, $password, $admin, $api_key) public function update($id, $email, $password, $admin, $api_key, $status)
{ {
$user = [ $user = [
'email' => $email, 'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT), 'password' => password_hash($password, PASSWORD_DEFAULT),
'admin' => $admin, 'admin' => $admin,
'api_key' => $api_key, 'api_key' => $api_key,
'status' => $status,
]; ];
return $this->model_user->update($id, $user); return $this->model_user->update($id, $user);
@ -192,16 +208,18 @@ namespace controllers\internals;
* @param mixed $password * @param mixed $password
* @param mixed $admin * @param mixed $admin
* @param ?string $api_key : The api key of the user, if null generate randomly * @param ?string $api_key : The api key of the user, if null generate randomly
* @param string $status : User status, default \models\User::STATUS_ACTIVE
* *
* @return mixed bool|int : false on error, id of the new user else * @return mixed bool|int : false on error, id of the new user else
*/ */
public function create($email, $password, $admin, ?string $api_key = null) public function create($email, $password, $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE)
{ {
$user = [ $user = [
'email' => $email, 'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT), 'password' => password_hash($password, PASSWORD_DEFAULT),
'admin' => $admin, 'admin' => $admin,
'api_key' => $api_key ?? $this->generate_random_api_key(), 'api_key' => $api_key ?? $this->generate_random_api_key(),
'status' => $status,
]; ];
$new_user_id = $this->model_user->insert($user); $new_user_id = $this->model_user->insert($user);

View File

@ -30,6 +30,7 @@ namespace controllers\publics;
'INVALID_PARAMETER' => 2, 'INVALID_PARAMETER' => 2,
'MISSING_PARAMETER' => 4, 'MISSING_PARAMETER' => 4,
'CANNOT_CREATE' => 8, 'CANNOT_CREATE' => 8,
'SUSPENDED_USER' => 16,
]; ];
const ERROR_MESSAGES = [ const ERROR_MESSAGES = [
@ -37,6 +38,7 @@ namespace controllers\publics;
'INVALID_PARAMETER' => 'You have specified an invalid parameter : ', 'INVALID_PARAMETER' => 'You have specified an invalid parameter : ',
'MISSING_PARAMETER' => 'One require parameter is missing : ', 'MISSING_PARAMETER' => 'One require parameter is missing : ',
'CANNOT_CREATE' => 'Cannot create a new entry.', 'CANNOT_CREATE' => 'Cannot create a new entry.',
'SUSPENDED_USER' => 'This user account is currently suspended.',
]; ];
private $internal_user; private $internal_user;
@ -86,6 +88,17 @@ namespace controllers\publics;
exit(self::ERROR_CODES['INVALID_CREDENTIALS']); exit(self::ERROR_CODES['INVALID_CREDENTIALS']);
} }
if ($this->user['status'] !== \models\User::STATUS_ACTIVE)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['SUSPENDED_USER'];
$return['message'] = self::ERROR_MESSAGES['SUSPENDED_USER'];
$this->auto_http_code(false);
$this->json($return);
exit(self::ERROR_CODES['SUSPENDED_USER']);
}
} }
/** /**

View File

@ -64,6 +64,13 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Connect', 'login')); return $this->redirect(\descartes\Router::url('Connect', 'login'));
} }
if ($user['status'] !== \models\User::STATUS_ACTIVE)
{
\FlashMessage\FlashMessage::push('danger', 'Votre compte est actuellement suspendu.');
return $this->redirect(\descartes\Router::url('Connect', 'login'));
}
$settings = $this->internal_setting->gets_for_user($user['id']); $settings = $this->internal_setting->gets_for_user($user['id']);
$user['settings'] = $settings; $user['settings'] = $settings;

View File

@ -48,6 +48,44 @@ class User extends \descartes\Controller
$users = $this->internal_user->list(25, $page); $users = $this->internal_user->list(25, $page);
$this->render('user/list', ['users' => $users]); $this->render('user/list', ['users' => $users]);
} }
/**
* Update status of users
*
* @param array int $_GET['ids'] : User ids
* @param mixed $csrf
* @param int $status : 1 -> active, 0 -> suspended
*
* @return boolean;
*/
public function update_status ($csrf, int $status)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('User', 'list'));
}
if ($status == 0)
{
$status = \models\User::STATUS_SUSPENDED;
}
else
{
$status = \models\User::STATUS_ACTIVE;
}
$ids = $_GET['ids'] ?? [];
foreach ($ids as $id)
{
$this->internal_user->update_status($id, $status);
}
return $this->redirect(\descartes\Router::url('User', 'list'));
}
/** /**
* Cette fonction va supprimer une liste de users. * Cette fonction va supprimer une liste de users.
@ -112,6 +150,7 @@ class User extends \descartes\Controller
$email = $_POST['email'] ?? false; $email = $_POST['email'] ?? false;
$password = $_POST['password'] ?? \controllers\internals\Tool::generate_password(rand(6, 12)); $password = $_POST['password'] ?? \controllers\internals\Tool::generate_password(rand(6, 12));
$admin = $_POST['admin'] ?? false; $admin = $_POST['admin'] ?? false;
$status = 'active';
if (!$email) if (!$email)
{ {
@ -127,7 +166,7 @@ class User extends \descartes\Controller
return $this->redirect(\descartes\Router::url('User', 'add')); return $this->redirect(\descartes\Router::url('User', 'add'));
} }
$user_id = $this->internal_user->create($email, $password, $admin); $user_id = $this->internal_user->create($email, $password, $admin, $status);
if (!$user_id) if (!$user_id)
{ {
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce user.'); \FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce user.');

View File

@ -0,0 +1,38 @@
<?php
use Phinx\Migration\AbstractMigration;
class AddUserStatus extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* Any other destructive changes will result in an error when trying to
* rollback the migration.
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('user');
$table->addColumn('status', 'enum', ['values' => ['suspended', 'active']]);
$table->update();
}
}

View File

@ -16,6 +16,9 @@ namespace models;
*/ */
class User extends \descartes\Model class User extends \descartes\Model
{ {
const STATUS_SUSPENDED = 'suspended';
const STATUS_ACTIVE = 'active';
/** /**
* Find a user by his id. * Find a user by his id.
* *

View File

@ -156,6 +156,7 @@
'add' => '/user/add/', 'add' => '/user/add/',
'create' => '/user/create/{csrf}/', 'create' => '/user/create/{csrf}/',
'delete' => '/user/delete/{csrf}/', 'delete' => '/user/delete/{csrf}/',
'update_status' => '/user/delete/{status}/{csrf}/',
], ],
'Phone' => [ 'Phone' => [

View File

@ -42,6 +42,7 @@
<th>#</th> <th>#</th>
<th>Email</th> <th>Email</th>
<th>Admin</th> <th>Admin</th>
<th>Status</th>
<th style="width:5%;">Sélectionner</th> <th style="width:5%;">Sélectionner</th>
</tr> </tr>
</thead> </thead>
@ -51,6 +52,7 @@
<td><?php $this->s($user['id']); ?></td> <td><?php $this->s($user['id']); ?></td>
<td><?php $this->s($user['email']); ?></td> <td><?php $this->s($user['email']); ?></td>
<td><?php $this->s($user['admin']); ?></td> <td><?php $this->s($user['admin']); ?></td>
<td><?php $this->s($user['status']); ?></td>
<td><input type="checkbox" value="<?php $this->s($user['id']); ?>" name="ids[]"></td> <td><input type="checkbox" value="<?php $this->s($user['id']); ?>" name="ids[]"></td>
</tr> </tr>
<?php } ?> <?php } ?>
@ -63,6 +65,8 @@
</div> </div>
<div class="text-right col-xs-6 no-padding"> <div class="text-right col-xs-6 no-padding">
<strong>Action pour la séléction :</strong> <strong>Action pour la séléction :</strong>
<button class="btn btn-default" type="submit" formaction="<?php echo \descartes\Router::url('User', 'update_status', ['csrf' => $_SESSION['csrf'], 'status' => 0]); ?>"><span class="fa fa-pause"></span> Suspendre</button>
<button class="btn btn-default" type="submit" formaction="<?php echo \descartes\Router::url('User', 'update_status', ['csrf' => $_SESSION['csrf'], 'status' => 1]); ?>"><span class="fa fa-play"></span> Activer</button>
<button class="btn btn-default" type="submit" formaction="<?php echo \descartes\Router::url('User', 'delete', ['csrf' => $_SESSION['csrf']]); ?>"><span class="fa fa-trash-o"></span> Supprimer</button> <button class="btn btn-default" type="submit" formaction="<?php echo \descartes\Router::url('User', 'delete', ['csrf' => $_SESSION['csrf']]); ?>"><span class="fa fa-trash-o"></span> Supprimer</button>
</div> </div>
</div> </div>