<?php
 namespace Extension;
 use TCMS\AuthExtension;
 class ChocolaAuthDefault extends AuthExtension {
 private $option = [];
 private $userId = null;
 public function __construct() {
 include_library('Cookie');
 $this->loginUrl = base_url('/login');
 \Model::add('GroupUserModel', \VnChocola\Model\GroupUserModel::class, true);
 \Model::add('BranchsModel', \VnChocola\Model\BranchsModel::class, true);
 \Model::add('UserModel');
 }
 public function login(string $username, string $password, ?array $option = null) {
 copy_option($this->option, $option);
 include_library('HttpRequest');
 $http = new \HttpRequest(API_URL . '/getToken', 'POST');
 $http->setContentType('application/x-www-form-urlencoded');
 $http->setPostData([ 'username' => $username, 'password' => $password ]);
 $res = $http->execute();
 if($res) {
 $res = json_decode($res);
 if(isset($res->token)) {
 \Cookie::set('_apitoken', $res->token, 86400);
 \Cookie::set('_loginInfo', json_encode([ 'id' => $res->id, 'name' => $res->name, 'loginDate' => date('Y-m-d', time()) ]), 86500);
 return true;
 }
 }
 return $http;
 }
 public function logout() {
 \Cookie::remove('_apitoken');
 \Cookie::remove('_loginInfo');
 }
 public function auth() {
 if(!\Cookie::has('_apitoken')) {
 return false;
 }
 if(!\Cookie::has('_loginInfo')) {
 return false;
 }
 $info = json_decode(\Cookie::get('_loginInfo'));
 if(!$info || !isset($info->loginDate)) {
 return false;
 }
 $this->userId = $info->id;
 $dt = date('Y-m-d', time());
 if($info->loginDate != $dt) {
 return false;
 }
 return true;
 }
 public function getName() {
 if(!\Cookie::has('_loginInfo')) {
 return false;
 }
 return json_decode(\Cookie::get('_loginInfo'))->name;
 }
 private $groupRoles = null;
 private $userCustomUser = null;
 public function hasRole($role) {
 if(!$this->userId) return false;
 if(!$this->userCustomUser) {
 $userModel = \Model::load('UserModel');
 $u = $userModel->find($this->userId);
 if(!$u) throw new \TCMS\TException("Không tìm thấy thông tin người dùng đang đăng nhập", 500);
 require_once(APP_PATH . 'models' . DIRECTORY_SEPARATOR . 'CustomUserModel.php');
 $this->userCustomUser = new \CustomUserModel();
 $this->userCustomUser->copy($u);
 }
 if(!$this->userCustomUser) return false;
 if(!$this->groupRoles) {
 $model = \Model::load('GroupUserModel');
 $group = $model->select('roles')->where('id', $this->userCustomUser->groupId)->first();
 if(!$group) return false;
 $this->groupRoles = $group->roles;
 }
 return strpos($this->groupRoles, ','. $role .',') !== false;
 }
 public function getUser() {
 if($this->userCustomUser) return $this->userCustomUser;
 if(!$this->userId) return false;
 $userModel = \Model::load('UserModel');
 $user = $userModel->find($this->userId);
 if(!$user) throw new \TCMS\TException("Không tìm thấy thông tin người dùng đang đăng nhập", 500);
 require_once(APP_PATH . 'models' . DIRECTORY_SEPARATOR . 'CustomUserModel.php');
 $this->userCustomUser = new \CustomUserModel();
 $this->userCustomUser->copy($user);
 return $this->userCustomUser;
 }
 public function encodePass($password) {
 $secretKey = 'a@d12s01@2sdc!mpc213212ss1tvthoa';
 $iv = substr($secretKey, 0,16);
 $encrypted = openssl_encrypt($password, 'AES-256-CBC', $secretKey,0,$iv);
 $hmac = hash_hmac('md5', $encrypted, $secretKey);
 return $encrypted;
 }
 }
