2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
2019-10-30 00:30:39 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace models;
|
2019-10-29 18:33:49 +01:00
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Class for user table administration. Not a standard model because has absolutly no user based restrictions.
|
2019-11-13 03:24:22 +01:00
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
class User extends \descartes\Model
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2020-03-30 01:52:53 +02:00
|
|
|
const STATUS_SUSPENDED = 'suspended';
|
|
|
|
const STATUS_ACTIVE = 'active';
|
|
|
|
|
2020-01-08 14:14:40 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Find a user by his id.
|
|
|
|
*
|
2020-01-08 14:14:40 +01:00
|
|
|
* @param string $id : User id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-08 14:14:40 +01:00
|
|
|
* @return mixed array
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function get($id)
|
2020-01-08 14:14:40 +01:00
|
|
|
{
|
|
|
|
return $this->_select_one('user', ['id' => $id]);
|
|
|
|
}
|
|
|
|
|
2021-06-12 23:23:15 +02:00
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* Find user by ids.
|
|
|
|
*
|
2021-06-12 23:23:15 +02:00
|
|
|
* @param array $ids : users ids
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function gets_in_by_id($ids)
|
|
|
|
{
|
|
|
|
if (!$ids)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
SELECT * FROM `user`
|
|
|
|
WHERE id ';
|
|
|
|
|
|
|
|
$params = [];
|
|
|
|
|
|
|
|
$generated_in = $this->_generate_in_from_array($ids);
|
|
|
|
$query .= $generated_in['QUERY'];
|
|
|
|
$params = $generated_in['PARAMS'];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Find a user using his email.
|
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @param string $email : User email
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @return mixed array
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function get_by_email($email)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_select_one('user', ['email' => $email]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Get a user by his api_key address.
|
|
|
|
*
|
2020-01-08 02:14:38 +01:00
|
|
|
* @param string $api_key : User api key
|
|
|
|
*
|
|
|
|
* @return mixed boolean | array : false if cannot find user for this api key, the user else
|
|
|
|
*/
|
|
|
|
public function get_by_api_key(string $api_key)
|
|
|
|
{
|
|
|
|
return $this->_select_one('user', ['api_key' => $api_key]);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Return list of user.
|
|
|
|
*
|
|
|
|
* @param int $limit : Number of user to return
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $offset : Number of user to skip
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
|
|
|
public function list($limit, $offset)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-10 00:27:42 +01:00
|
|
|
return $this->_select('user', [], null, false, $limit, $offset);
|
2019-10-29 18:33:49 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2021-01-14 03:32:17 +01:00
|
|
|
* Delete a user.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2020-10-22 20:31:01 +02:00
|
|
|
* @param int $id : Id de l'utilisateur a supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Le nombre de lignes supprimées
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
|
|
|
public function remove($id)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_delete('user', ['id' => $id]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Insert a new user.
|
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @param array $user : User to insert
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @return mixed bool|int : false if fail, new user id else
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function insert($user)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-13 03:24:22 +01:00
|
|
|
$success = $this->_insert('user', $user);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
return $success ? $this->_last_id() : false;
|
|
|
|
}
|
2019-11-13 03:24:22 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a user using his is.
|
|
|
|
*
|
2021-02-23 00:31:54 +01:00
|
|
|
* @param int $id : User id
|
2021-01-17 03:16:57 +01:00
|
|
|
* @param array $data : Data to update
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @return int : number of modified rows
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
public function update($id, $data)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2021-01-17 03:16:57 +01:00
|
|
|
return $this->_update('user', $data, ['id' => $id]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Update a user password by his id.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param int $id : User id
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $password : The new password of the user
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Number of modified lines
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function update_password($id, $password)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_update('user', ['password' => $password], ['id' => $id]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Update a user email by his id.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param int $id : User id
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $email : The new email
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Number of modified lines
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function update_email($id, $email)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_update('user', ['email' => $email], ['id' => $id]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
}
|