dolibarr  13.0.2
api_login.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
22 
26 class Login
27 {
28 
32  public function __construct()
33  {
34  global $db;
35  $this->db = $db;
36  }
37 
57  public function index($login, $password, $entity = '', $reset = 0)
58  {
59  global $conf, $dolibarr_main_authentication, $dolibarr_auto_user;
60 
61  // TODO Remove the API login. The token must be generated from backoffice only.
62 
63  // Authentication mode
64  if (empty($dolibarr_main_authentication)) $dolibarr_main_authentication = 'dolibarr';
65 
66  // Authentication mode: forceuser
67  if ($dolibarr_main_authentication == 'forceuser')
68  {
69  if (empty($dolibarr_auto_user)) $dolibarr_auto_user = 'auto';
70  if ($dolibarr_auto_user != $login)
71  {
72  dol_syslog("Warning: your instance is set to use the automatic forced login '".$dolibarr_auto_user."' that is not the requested login. API usage is forbidden in this mode.");
73  throw new RestException(403, "Your instance is set to use the automatic login '".$dolibarr_auto_user."' that is not the requested login. API usage is forbidden in this mode.");
74  }
75  }
76 
77  // Set authmode
78  $authmode = explode(',', $dolibarr_main_authentication);
79 
80  if ($entity != '' && !is_numeric($entity))
81  {
82  throw new RestException(403, "Bad value for entity, must be the numeric ID of company.");
83  }
84  if ($entity == '') $entity = 1;
85 
86  include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
87  $login = checkLoginPassEntity($login, $password, $entity, $authmode, 'api');
88  if (empty($login))
89  {
90  throw new RestException(403, 'Access denied');
91  }
92 
93  $token = 'failedtogenerateorgettoken';
94 
95  $tmpuser = new User($this->db);
96  $tmpuser->fetch(0, $login, 0, 0, $entity);
97  if (empty($tmpuser->id))
98  {
99  throw new RestException(500, 'Failed to load user');
100  }
101 
102  // Renew the hash
103  if (empty($tmpuser->api_key) || $reset)
104  {
105  $tmpuser->getrights();
106  if (empty($tmpuser->rights->user->self->creer))
107  {
108  throw new RestException(403, 'User need write permission on itself to reset its API token');
109  }
110 
111  // Generate token for user
112  $token = dol_hash($login.uniqid().(empty($conf->global->MAIN_API_KEY)?'':$conf->global->MAIN_API_KEY), 1);
113 
114  // We store API token into database
115  $sql = "UPDATE ".MAIN_DB_PREFIX."user";
116  $sql .= " SET api_key = '".$this->db->escape($token)."'";
117  $sql .= " WHERE login = '".$this->db->escape($login)."'";
118 
119  dol_syslog(get_class($this)."::login", LOG_DEBUG); // No log
120  $result = $this->db->query($sql);
121  if (!$result)
122  {
123  throw new RestException(500, 'Error when updating api_key for user :'.$this->db->lasterror());
124  }
125  } else {
126  $token = $tmpuser->api_key;
127  }
128 
129  //return token
130  return array(
131  'success' => array(
132  'code' => 200,
133  'token' => $token,
134  'entity' => $tmpuser->entity,
135  'message' => 'Welcome '.$login.($reset ? ' - Token is new' : ' - This is your token (recorded for your user). You can use it to make any REST API call, or enter it into the DOLAPIKEY field to use the Dolibarr API explorer.')
136  )
137  );
138  }
139 }
dol_hash($chain, $type= '0')
Returns a hash of a string.
Class to manage Dolibarr users.
Definition: user.class.php:44
index($login, $password, $entity= '', $reset=0)
Login.
API that allows to log in with an user account.
$conf db
API class for accounts.
Definition: inc.php:54
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
checkLoginPassEntity($usertotest, $passwordtotest, $entitytotest, $authmode, $context= '')
Return a login if login/pass was successfull.
__construct()
Constructor of the class.