PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/prestashop_1.5.6.2/prestashop/classes/Employee.php

https://gitlab.com/casiazul/colectivoweb
PHP | 347 lines | 208 code | 46 blank | 93 comment | 50 complexity | 28d1ce5ff9693d3a6d3f3bedd6b5fc14 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2013 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2013 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class EmployeeCore extends ObjectModel
  27. {
  28. public $id;
  29. /** @var string Determine employee profile */
  30. public $id_profile;
  31. /** @var string employee language */
  32. public $id_lang;
  33. /** @var string Lastname */
  34. public $lastname;
  35. /** @var string Firstname */
  36. public $firstname;
  37. /** @var string e-mail */
  38. public $email;
  39. /** @var string Password */
  40. public $passwd;
  41. /** @var datetime Password */
  42. public $last_passwd_gen;
  43. public $stats_date_from;
  44. public $stats_date_to;
  45. /** @var string Display back office background in the specified color */
  46. public $bo_color;
  47. public $default_tab;
  48. /** @var string employee's chosen theme */
  49. public $bo_theme;
  50. /** @var integer employee desired screen width */
  51. public $bo_width;
  52. /** @var bool, true */
  53. public $bo_show_screencast;
  54. /** @var boolean Status */
  55. public $active = 1;
  56. public $remote_addr;
  57. /**
  58. * @see ObjectModel::$definition
  59. */
  60. public static $definition = array(
  61. 'table' => 'employee',
  62. 'primary' => 'id_employee',
  63. 'fields' => array(
  64. 'lastname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
  65. 'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
  66. 'email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 128),
  67. 'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
  68. 'passwd' => array('type' => self::TYPE_STRING, 'validate' => 'isPasswdAdmin', 'required' => true, 'size' => 32),
  69. 'last_passwd_gen' => array('type' => self::TYPE_STRING),
  70. 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  71. 'id_profile' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true),
  72. 'bo_color' => array('type' => self::TYPE_STRING, 'validate' => 'isColor', 'size' => 32),
  73. 'default_tab' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
  74. 'bo_theme' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 32),
  75. 'bo_width' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
  76. 'bo_show_screencast' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  77. 'stats_date_from' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  78. 'stats_date_to' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  79. ),
  80. );
  81. protected $webserviceParameters = array(
  82. 'fields' => array(
  83. 'id_lang' => array('xlink_resource' => 'languages'),
  84. 'last_passwd_gen' => array('setter' => null),
  85. 'stats_date_from' => array('setter' => null),
  86. 'stats_date_to' => array('setter' => null),
  87. 'passwd' => array('setter' => 'setWsPasswd'),
  88. ),
  89. );
  90. protected $associated_shops = array();
  91. public function __construct($id = null, $id_lang = null, $id_shop = null)
  92. {
  93. parent::__construct($id, $id_lang, $id_shop);
  94. if ($this->id)
  95. $this->associated_shops = $this->getAssociatedShops();
  96. }
  97. /**
  98. * @see ObjectModel::getFields()
  99. * @return array
  100. */
  101. public function getFields()
  102. {
  103. if (empty($this->stats_date_from) || $this->stats_date_from == '0000-00-00')
  104. $this->stats_date_from = date('Y-m-d', strtotime("-1 month"));
  105. if (empty($this->stats_compare_from) || $this->stats_compare_from == '0000-00-00')
  106. $this->stats_compare_from = null;
  107. if (empty($this->stats_date_to) || $this->stats_date_to == '0000-00-00')
  108. $this->stats_date_to = date('Y-m-d');
  109. if (empty($this->stats_compare_to) || $this->stats_compare_to == '0000-00-00')
  110. $this->stats_compare_to = null;
  111. return parent::getFields();
  112. }
  113. public function add($autodate = true, $null_values = true)
  114. {
  115. $this->last_passwd_gen = date('Y-m-d H:i:s', strtotime('-'.Configuration::get('PS_PASSWD_TIME_BACK').'minutes'));
  116. return parent::add($autodate, $null_values);
  117. }
  118. public function update($null_values = false)
  119. {
  120. if (empty($this->stats_date_from) || $this->stats_date_from == '0000-00-00')
  121. $this->stats_date_from = date('Y-m-d');
  122. if (empty($this->stats_date_to) || $this->stats_date_to == '0000-00-00')
  123. $this->stats_date_to = date('Y-m-d');
  124. return parent::update($null_values);
  125. }
  126. /**
  127. * Return list of employees
  128. */
  129. public static function getEmployees()
  130. {
  131. return Db::getInstance()->executeS('
  132. SELECT `id_employee`, `firstname`, `lastname`
  133. FROM `'._DB_PREFIX_.'employee`
  134. WHERE `active` = 1
  135. ORDER BY `lastname` ASC
  136. ');
  137. }
  138. /**
  139. * Return employee instance from its e-mail (optionnaly check password)
  140. *
  141. * @param string $email e-mail
  142. * @param string $passwd Password is also checked if specified
  143. * @return Employee instance
  144. */
  145. public function getByEmail($email, $passwd = null)
  146. {
  147. if (!Validate::isEmail($email) || ($passwd != null && !Validate::isPasswd($passwd)))
  148. die(Tools::displayError());
  149. $result = Db::getInstance()->getRow('
  150. SELECT *
  151. FROM `'._DB_PREFIX_.'employee`
  152. WHERE `active` = 1
  153. AND `email` = \''.pSQL($email).'\'
  154. '.($passwd ? 'AND `passwd` = \''.Tools::encrypt($passwd).'\'' : ''));
  155. if (!$result)
  156. return false;
  157. $this->id = $result['id_employee'];
  158. $this->id_profile = $result['id_profile'];
  159. foreach ($result as $key => $value)
  160. if (property_exists($this, $key))
  161. $this->{$key} = $value;
  162. return $this;
  163. }
  164. public static function employeeExists($email)
  165. {
  166. if (!Validate::isEmail($email))
  167. die (Tools::displayError());
  168. return (bool)Db::getInstance()->getValue('
  169. SELECT `id_employee`
  170. FROM `'._DB_PREFIX_.'employee`
  171. WHERE `email` = \''.pSQL($email).'\'');
  172. }
  173. /**
  174. * Check if employee password is the right one
  175. *
  176. * @param string $passwd Password
  177. * @return boolean result
  178. */
  179. public static function checkPassword($id_employee, $passwd)
  180. {
  181. if (!Validate::isUnsignedId($id_employee) || !Validate::isPasswd($passwd, 8))
  182. die (Tools::displayError());
  183. return Db::getInstance()->getValue('
  184. SELECT `id_employee`
  185. FROM `'._DB_PREFIX_.'employee`
  186. WHERE `id_employee` = '.(int)$id_employee.'
  187. AND `passwd` = \''.pSQL($passwd).'\'
  188. AND active = 1');
  189. }
  190. public static function countProfile($id_profile, $active_only = false)
  191. {
  192. return Db::getInstance()->getValue('
  193. SELECT COUNT(*)
  194. FROM `'._DB_PREFIX_.'employee`
  195. WHERE `id_profile` = '.(int)$id_profile.'
  196. '.($active_only ? ' AND `active` = 1' : ''));
  197. }
  198. public function isLastAdmin()
  199. {
  200. return ($this->isSuperAdmin()
  201. && Employee::countProfile($this->id_profile, true) == 1
  202. && $this->active
  203. );
  204. }
  205. public function setWsPasswd($passwd)
  206. {
  207. if ($this->id != 0)
  208. {
  209. if ($this->passwd != $passwd)
  210. $this->passwd = Tools::encrypt($passwd);
  211. }
  212. else
  213. $this->passwd = Tools::encrypt($passwd);
  214. return true;
  215. }
  216. /**
  217. * Check employee informations saved into cookie and return employee validity
  218. *
  219. * @return boolean employee validity
  220. */
  221. public function isLoggedBack()
  222. {
  223. if (!Cache::isStored('isLoggedBack'.$this->id))
  224. {
  225. /* Employee is valid only if it can be load and if cookie password is the same as database one */
  226. Cache::store('isLoggedBack'.$this->id, (
  227. $this->id && Validate::isUnsignedId($this->id) && Employee::checkPassword($this->id, Context::getContext()->cookie->passwd)
  228. && (!isset(Context::getContext()->cookie->remote_addr) || Context::getContext()->cookie->remote_addr == ip2long(Tools::getRemoteAddr()) || !Configuration::get('PS_COOKIE_CHECKIP'))
  229. ));
  230. }
  231. return Cache::retrieve('isLoggedBack'.$this->id);
  232. }
  233. /**
  234. * Logout
  235. */
  236. public function logout()
  237. {
  238. if (isset(Context::getContext()->cookie))
  239. {
  240. Context::getContext()->cookie->logout();
  241. Context::getContext()->cookie->write();
  242. }
  243. $this->id = null;
  244. }
  245. /**
  246. * Check if the employee is associated to a specific shop
  247. *
  248. * @since 1.5.0
  249. * @param int $id_shop
  250. * @return bool
  251. */
  252. public function hasAuthOnShop($id_shop)
  253. {
  254. return $this->isSuperAdmin() || in_array($id_shop, $this->associated_shops);
  255. }
  256. /**
  257. * Check if the employee is associated to a specific shop group
  258. *
  259. * @since 1.5.0
  260. * @param int $id_shop_shop
  261. * @return bool
  262. */
  263. public function hasAuthOnShopGroup($id_shop_group)
  264. {
  265. if ($this->isSuperAdmin())
  266. return true;
  267. foreach ($this->associated_shops as $id_shop)
  268. if ($id_shop_group == Shop::getGroupFromShop($id_shop, true))
  269. return true;
  270. return false;
  271. }
  272. /**
  273. * Get default id_shop with auth for current employee
  274. *
  275. * @since 1.5.0
  276. * @return int
  277. */
  278. public function getDefaultShopID()
  279. {
  280. if ($this->isSuperAdmin() || in_array(Configuration::get('PS_SHOP_DEFAULT'), $this->associated_shops))
  281. return Configuration::get('PS_SHOP_DEFAULT');
  282. return $this->associated_shops[0];
  283. }
  284. public static function getEmployeesByProfile($id_profile, $active_only = false)
  285. {
  286. return Db::getInstance()->executeS('
  287. SELECT *
  288. FROM `'._DB_PREFIX_.'employee`
  289. WHERE `id_profile` = '.(int)$id_profile.'
  290. '.($active_only ? ' AND `active` = 1' : ''));
  291. }
  292. /**
  293. * Check if current employee is super administrator
  294. *
  295. * @return bool
  296. */
  297. public function isSuperAdmin()
  298. {
  299. return $this->id_profile == _PS_ADMIN_PROFILE_;
  300. }
  301. }