raspisms/controllers/internals/User.php

192 lines
5.3 KiB
PHP
Raw Normal View History

2019-10-29 14:57:13 +01:00
<?php
/*
2019-11-10 17:48:54 +01:00
* This file is part of RaspiSMS.
*
2019-11-10 17:48:54 +01:00
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
2019-11-10 17:48:54 +01:00
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace controllers\internals;
2019-10-29 14:57:13 +01:00
/**
* Methods to manage user. Not a standard controller as it has nothing to do with user based restrictions and must be usable only by admin
*/
2019-10-29 18:36:25 +01:00
class User extends \descartes\InternalController
{
2019-10-29 14:57:13 +01:00
private $model_user;
private $internal_event;
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
public function __construct(\PDO $bdd)
2019-10-29 14:57:13 +01:00
{
$this->model_user = new \models\User($bdd);
$this->internal_event = new \controllers\internals\Event($bdd);
}
2019-10-29 18:36:25 +01:00
/**
* Return list of users as an array.
*
2019-10-29 14:57:13 +01:00
* @param mixed(int|bool) $nb_entry : Number of entry to return
* @param mixed(int|bool) $page : Numero of page
*
2019-10-29 14:57:13 +01:00
* @return array|bool : List of user or false
2019-10-29 18:36:25 +01:00
*/
public function list(?int $nb_entry = null, ?int $page = null)
2019-10-29 14:57:13 +01:00
{
2019-11-12 17:37:20 +01:00
return $this->model_user->list($nb_entry, $page * $nb_entry);
2019-10-29 14:57:13 +01:00
}
2019-10-29 14:57:13 +01:00
/**
* Delete a user.
*
2019-10-29 18:36:25 +01:00
* @param array $ids : Les id des useres à supprimer
* @param mixed $id
*
* @return int : Number of users deleted
2019-10-29 18:36:25 +01:00
*/
public function delete($id)
2019-10-29 14:57:13 +01:00
{
return $this->model_user->remove($id);
2019-10-29 18:36:25 +01:00
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
/**
* Check user credentials
*
* @param string $email : User email
* @param string $password : User password
*
* @return mixed false | array : False if no user for thoses credentials, the user else
2019-10-29 18:36:25 +01:00
*/
public function check_credentials($email, $password)
2019-10-29 14:57:13 +01:00
{
$user = $this->model_user->get_by_email($email);
if (!$user)
{
2019-10-29 14:57:13 +01:00
return false;
}
if (!password_verify($password, $user['password']))
{
2019-10-29 14:57:13 +01:00
return false;
}
return $user;
2019-10-29 18:36:25 +01:00
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
/**
* Update a user password.
*
* @param string $id : User id
2019-10-29 14:57:13 +01:00
* @param string $password : New password
*
* @return bool;
2019-10-29 18:36:25 +01:00
*/
public function update_password(int $id, string $password): bool
2019-10-29 18:36:25 +01:00
{
2019-10-29 14:57:13 +01:00
$password = password_hash($password, PASSWORD_DEFAULT);
2019-10-29 14:57:13 +01:00
return (bool) $this->model_user->update_password($id, $password);
}
2019-10-29 14:57:13 +01:00
/**
* Update a user transfer property value.
*
* @param string $id : User id
2019-10-29 14:57:13 +01:00
* @param string $transfer : New value of property transfer
*
2019-10-29 18:36:25 +01:00
* @return boolean;
*/
public function update_transfer(int $id, int $transfer): bool
2019-10-29 18:36:25 +01:00
{
2019-10-29 14:57:13 +01:00
return (bool) $this->model_user->update_transfer($id, $transfer);
}
2019-10-29 14:57:13 +01:00
/**
* Update user email.
*
* @param string $id : user id
2019-10-29 14:57:13 +01:00
* @param string $email : new mail
*
2019-10-29 18:36:25 +01:00
* @return boolean;
*/
public function update_email($id, $email)
{
2019-10-29 14:57:13 +01:00
return (bool) $this->model_user->update_email($id, $email);
}
/**
* Get a user by his email address
* @param string $email : User email
*
* @return mixed boolean | array : false if cannot find user for this email, the user else
2019-10-29 14:57:13 +01:00
*/
2019-10-29 18:36:25 +01:00
public function get_by_email($email)
2019-10-29 14:57:13 +01:00
{
return $this->model_user->get_by_email($email);
2019-10-29 14:57:13 +01:00
}
/**
* Return users by transfer status.
*
* @param bool $transfer : transfer status
*/
public function gets_by_transfer($transfer)
{
return $this->model_user->get_by_transfer($transfer);
}
2019-10-29 18:36:25 +01:00
/**
* Update a user by his id
* @param mixed $id
* @param mixed $email
* @param mixed $password
* @param mixed $admin
* @param mixed $transfer
*
* @return int : Number of modified user
2019-10-29 18:36:25 +01:00
*/
public function update($id, $email, $password, $admin, $transfer)
2019-10-29 14:57:13 +01:00
{
$user = [
'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT),
'admin' => $admin,
'transfer' => $transfer,
];
return $this->model_user->update($id, $user);
2019-10-29 14:57:13 +01:00
}
2019-10-29 14:57:13 +01:00
/**
* Create a new user
*
* @param mixed $email
* @param mixed $password
* @param mixed $admin
* @param mixed $transfer
*
* @return mixed bool|int : false on error, id of the new user else
2019-10-29 18:36:25 +01:00
*/
public function create($email, $password, $admin, $transfer = false)
2019-10-29 14:57:13 +01:00
{
$user = [
'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT),
'admin' => $admin,
'transfer' => $transfer,
];
$result = $this->model_user->insert($user);
2019-10-29 14:57:13 +01:00
if (!$result)
{
2019-10-29 14:57:13 +01:00
return false;
}
return $result;
2019-10-29 18:36:25 +01:00
}
}