gy-doc-code
[ class tree: gy-doc-code ] [ index: gy-doc-code ] [ all elements ]

Source for file User.php

Documentation is available at User.php

  1. <?php
  2.  
  3. namespace Gy\Core\User;
  4.  
  5. use Gy\Core\User\AccessUserGroup;
  6.  
  7. if (!defined("GY_CORE") && (GY_CORE !== true)) die( "gy: err include core" );
  8.  
  9. class User
  10. { 
  11.  
  12.     protected $authorized = false;
  13.     protected $dataUser;
  14.     protected $nameCookie = 'gy_user_auth';
  15.     protected $admin = false; 
  16.     public $tableName = 'users';
  17.  
  18.     public function __construct()
  19.     {
  20.         $this->checkUserCookie();
  21.     }
  22.  
  23.     /**
  24.      * getThisUserGroups - получить группы текущего пользователя
  25.      * @return array 
  26.      */
  27.     public function getThisUserGroups()
  28.     {
  29.         $arResult = array();
  30.         if (!empty($this->dataUser['groups'])) {
  31.             $arResult = $this->dataUser['groups'];
  32.         }
  33.         return $arResult;
  34.     }
  35.  
  36.     /**
  37.      * getDataThisUser - получить данные по текущему, авторизованному пользователю
  38.      * @return array 
  39.      */
  40.     public function getDataThisUser()
  41.     {
  42.         return $this->dataUser;
  43.     }
  44.  
  45.     /**
  46.      * isAdmin - проверить является ли текущий, авторизованный пользователем администратором
  47.      * @return booleand 
  48.      */
  49.     public function isAdmin()
  50.     {
  51.         return $this->admin;
  52.     }
  53.  
  54.     /**
  55.      * getAuthorized - узнать авторизован ли пользователь
  56.      * @return booleand 
  57.      */
  58.     public function getAuthorized()
  59.     {
  60.         return $this->authorized;
  61.     }
  62.  
  63.     /**
  64.      * getId - получить id текущего пользователя
  65.      * @return int 
  66.      */
  67.     public function getId(){
  68.         return $this->dataUser['id'];
  69.     }
  70.  
  71.     /**
  72.      * authorized - авторизовать пользователя
  73.      * @param type $log - логин
  74.      * @param type $pass - пароль
  75.      * @return booleand 
  76.      */
  77.     public function authorized($log, $pass )
  78.     {
  79.         $result = $this->chackUser($log, $pass);
  80.         $this->authorized = $result;
  81.         return $result;
  82.     }
  83.  
  84.     /**
  85.      * chackUser - проверить существует ли пользователь
  86.      * @global type $DB 
  87.      * @global type $CRYPTO 
  88.      * @param type $log - логин
  89.      * @param type $pass - пароль
  90.      * @return booleand 
  91.      */
  92.     protected function chackUser($log, $pass)
  93.     {
  94.         $result = false;
  95.  
  96.         global $DB;
  97.         global $CRYPTO;
  98.  
  99.         $res = $DB->selectDb(
  100.             $this->tableName, 
  101.             array('*'),
  102.             array(
  103.                 'AND' => array( 
  104.                     array('=' => array('login', "'".$log."'" ) ),
  105.                     array( '=' => array('pass',"'".md5($pass.$CRYPTO->getSole())."'") )
  106.                 ),
  107.             )
  108.         );
  109.  
  110.         if ($arRes = $DB->fetch($res)) {
  111.  
  112.             //$this->setUserCookie($arRes['id'] , $CRYPTO->getRandString());
  113.             $this->setUserCookie($arRes['id'] , $CRYPTO->getStringForUserCookie($arRes['login'], $arRes['name'], $arRes['id']));
  114.             $result = true;
  115.         }
  116.  
  117.         return $result;
  118.  
  119.     }
  120.  
  121.     /**
  122.      * setUserCookie - установить пользовательскую куку
  123.      * @global type $DB 
  124.      * @param int $userId - id пользователя
  125.      * @param string $StringCookie - строка, значение куки
  126.      * @return boolean 
  127.      */
  128.     protected function setUserCookie($userId, $StringCookie)
  129.     {
  130.         setcookie($this->nameCookie, $StringCookie, 0, '/');
  131.         global $DB;
  132.  
  133.         $res = $DB->updateDb(
  134.             $this->tableName,
  135.             array('hash_auth' => $StringCookie),
  136.             array( '=' => array('id' , $userId ) )
  137.         );
  138.  
  139.         return true;
  140.     }
  141.  
  142.     /**
  143.      * deleteUserCookie - удалить пользовательскую куку
  144.      * @global type $_COOKIE 
  145.      * @global type $DB 
  146.      * @param int $userId - id пользователя
  147.      * @return boolean 
  148.      */
  149.     protected function deleteUserCookie($userId)
  150.     {
  151.         global $_COOKIE;
  152.         unset($_COOKIE[$this->nameCookie]);
  153.         global $DB;
  154.  
  155.         $res = $DB->updateDb(
  156.             $this->tableName, 
  157.             array('hash_auth' => 'NULL'),
  158.             array( '=' => array('id' , $userId ) )
  159.         );
  160.  
  161.         return true;
  162.     }
  163.  
  164.     /**
  165.      * checkUserCookie - проверить пользовательскую куку
  166.      * @global type $_COOKIE 
  167.      * @return boolean 
  168.      */
  169.     public function checkUserCookie()
  170.     {
  171.         $result = false;
  172.  
  173.         global $_COOKIE;
  174.  
  175.         if (!empty($_COOKIE[$this->nameCookie])) {
  176.  
  177.             $dataUser = $this->findUserByCookie($_COOKIE[$this->nameCookie]);
  178.  
  179.             if ($dataUser !== false) {
  180.                 $this->dataUser = $dataUser;
  181.  
  182.                 // получить группы к каким относится пользователь
  183.                 $this->dataUser['groups'] = AccessUserGroup::getListGroupsByUser($dataUser['id']);
  184.  
  185.                 $this->authorized = true;
  186.                 if (!empty($this->dataUser['groups']['admins'])) {
  187.                     $this->admin = true;
  188.                 }
  189.                 $result = true;
  190.             }
  191.         }
  192.         return $result;
  193.     }
  194.  
  195.     /**
  196.      * findUserByCookie - найти пользователя по значению куки
  197.      * @global type $DB 
  198.      * @param string $cookie 
  199.      * @return array - данные пользователя
  200.      */
  201.     protected function findUserByCookie($cookie)
  202.     {
  203.         $result = false;
  204.  
  205.         global $DB;
  206.  
  207.         $res = $DB->selectDb(
  208.             $this->tableName,
  209.             array('*'), 
  210.             array( '=' => array('hash_auth', "'".$cookie."'") )
  211.         );
  212.  
  213.         if ($arRes = $DB->fetch($res)) {
  214.             $result = $arRes;
  215.         }
  216.  
  217.         return $result;
  218.     }
  219.  
  220.     /**
  221.      * userExit - сделать выход для пользователя
  222.      * @return boolean 
  223.      */
  224.     public function userExit()
  225.     {
  226.         return $this->deleteUserCookie($this->dataUser['id']);
  227.     }
  228.  
  229.     /**
  230.      * getAllDataUsers - получить данные по пользователю
  231.      * @global type $DB 
  232.      * @return array 
  233.      */
  234.     public function getAllDataUsers()
  235.     {
  236.         $result = array();
  237.  
  238.         global $DB;
  239.         $res = $DB->selectDb(
  240.             $this->tableName,
  241.             array('*')
  242.         );
  243.         $result = $DB->fetchAll($res, false);
  244.  
  245.         // получить группы пользователей
  246.         foreach ($result as $key => $value) {
  247.             $result[$key]['groups'] = AccessUserGroup::getListGroupsByUser($value['id']);
  248.         }
  249.  
  250.         return $result;
  251.     }
  252.  
  253.     /**
  254.      * getUserById - получить данные по пользователю по id
  255.      * @global type $DB 
  256.      * @param type $id 
  257.      * @return array 
  258.      */
  259.     public function getUserById($id)
  260.     {
  261.         $result = array();
  262.         global $DB;
  263.         $res = $DB->selectDb(
  264.             $this->tableName,
  265.             array('*'),
  266.             array(
  267.                 '=' => array('id', $id)
  268.             )
  269.         );
  270.         $result = $DB->fetch($res, false);
  271.  
  272.         if (!empty($result)) {
  273.             // получить группы текущего пользователя
  274.             $result['groups'] = AccessUserGroup::getListGroupsByUser($id);
  275.         }
  276.  
  277.         return $result;
  278.     }
  279.  
  280.     /**
  281.      * addUsers - добавить пользователя
  282.      * @global type $DB 
  283.      * @param type $data 
  284.      * @return boolean 
  285.      */
  286.     public function addUsers($data)
  287.     {
  288.         $result = false;
  289.  
  290.         // id, login, name, pass, groups
  291.         global $DB;
  292.         $res = $DB->insertDb($this->tableName, $data);
  293.  
  294.         if ($res) {
  295.             $result = true;
  296.         }
  297.  
  298.         return $result;
  299.     }
  300.  
  301.     /**
  302.      * updateUserById - обновление данных пользователя
  303.      * @global type $DB 
  304.      * @param int $userId - id пользователя
  305.      * @param array $arParams - данные пользователя
  306.      * @return boolean 
  307.      */
  308.     public function updateUserById($userId, $arParams)
  309.     {
  310.         $result = false;
  311.  
  312.         unset($arParams['id']);
  313.  
  314.         global $DB;
  315.         $res = $DB->updateDb($this->tableName, $arParams, array('=' => array('id', $userId)));
  316.  
  317.         if ($res) {
  318.             $result = true;
  319.         }
  320.  
  321.         return $result;
  322.     }
  323.  
  324.     /**
  325.      * deleteUserById - удалить пользователя
  326.      * @global type $DB 
  327.      * @param int $idUser - id пользователя
  328.      * @return string 
  329.      */
  330.     public function deleteUserById($idUser)
  331.     {
  332.         $result = false;
  333.  
  334.         if (is_numeric($idUser) && ($idUser != 1)) {
  335.             global $DB;
  336.  
  337.             $res = $DB->deleteDb($this->tableName, array('='=>array('id', $idUser)));
  338.  
  339.             if ($res) {
  340.                 $result = true;
  341.             }
  342.         }
  343.         return $result;
  344.     }
  345.  
  346. }

Documentation generated on Sat, 27 Apr 2024 11:17:30 +0300 by phpDocumentor 1.4.3