AdminUser.php 7.3 KB
Newer Older
T
Terry 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php

/*
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */

namespace fecshop\services\adminUser;

use Yii;
use fecshop\services\Service;

/**
 * AdminUser services. 用来给后台的用户提供数据。
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class AdminUser extends Service
{
    public $numPerPage = 20;
    /**
     *  language attribute.
     */
    protected $_lang_attr = [];
    protected $_modelName = '\fecshop\models\mysqldb\AdminUser';
    protected $_model;

    protected $_userFormModelName = '\fecshop\models\mysqldb\adminUser\AdminUserForm';
    protected $_userFormModel;
T
Terry 已提交
33 34 35
    
    protected $_userPassResetModelName = '\fecshop\models\mysqldb\adminUser\AdminUserResetPassword';
    protected $_userPassResetModel;
T
Terry 已提交
36 37 38 39 40 41

    public function init()
    {
        parent::init();
        list($this->_modelName, $this->_model) = \Yii::mapGet($this->_modelName);
        list($this->_userFormModelName, $this->_userFormModel) = \Yii::mapGet($this->_userFormModelName);
T
Terry 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55
        list($this->_userPassResetModelName, $this->_userPassResetModel) = \Yii::mapGet($this->_userPassResetModelName);
    }
    /**
     * @param $data array
     * @return boolean
     * update current user password
     */
    public function resetCurrentPassword($data){
        $this->_userPassResetModel->attributes = $data;
        if ($this->_userPassResetModel->validate()) {
			$this->_userPassResetModel->updatePassword();
            
            return true;
        } else {
T
Terry 已提交
56
            $errors = $this->_userPassResetModel->errors;
T
Terry 已提交
57 58 59 60
			Yii::$service->helper->errors->addByModelErrors($errors);
            
            return false;
        }        
T
Terry 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    }

    public function getPrimaryKey()
    {
        return 'id';
    }

    public function getActiveStatus(){
        $model = $this->_model;
        return $model::STATUS_ACTIVE;
    }
    public function getDeleteStatus(){
        $model = $this->_model;
        return $model::STATUS_DELETED;
    }

    public function getByPrimaryKey($primaryKey)
    {
        if ($primaryKey) {
            $one = $this->_model->findOne($primaryKey);
            foreach ($this->_lang_attr as $attrName) {
                if (isset($one[$attrName])) {
                    $one[$attrName] = unserialize($one[$attrName]);
                }
            }
            
            return $one;
        } else {
            
            return new $this->_modelName();
        }
    }
    /*
     * example filter:
     * [
     * 		'numPerPage' 	=> 20,
     * 		'pageNum'		=> 1,
     * 		'orderBy'	=> ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
            'where'			=> [
                ['>','price',1],
                ['<=','price',10]
     * 			['sku' => 'uk10001'],
     * 		],
     * 	'asArray' => true,
     * ]
     */
    public function coll($filter = '')
    {
        $query = $this->_model->find();
        $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
        $coll = $query->all();
        if (!empty($coll)) {
            foreach ($coll as $k => $one) {
                foreach ($this->_lang_attr as $attr) {
                    $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
                }
                $coll[$k] = $one;
            }
        }
        //var_dump($one);
        return [
            'coll' => $coll,
            'count'=> $query->limit(null)->offset(null)->count(),
        ];
    }

    /**
     * @param $data array, user form data
     * @param $roles array, role id array
     * @return boolean
     * 保存用户的信息,以及用户的role信息。
     */
    public function saveUserAndRole($data, $roles){
        $user_id = $this->save($data);
        if (!$user_id) {
            
            return false;
        }
        if (Yii::$service->admin->userRole->saveUserRole($user_id, $roles)) {
            
            return true;
        } 
        
        return false;
    }
    /**
     * @param $data array, user form data
     * @return mix ,return save user id | null
     * 保存用户的信息。
     */
    public function save($data) {
        $primaryKey = $this->getPrimaryKey();
        $user_id = 0;
        if ($data[$primaryKey]) {
            $this->_userFormModel = $this->_userFormModel->findOne($data[$primaryKey]);
        }
        $this->_userFormModel->attributes = $data;
        
        if (!$data['access_token']) {
            $this->_userFormModel->access_token = '';
        }
        if (!$data['auth_key']) {
            $this->_userFormModel->auth_key = '';
        }
T
Terry 已提交
165 166 167 168 169
        if (!$data['password'] && !$data['id']) {
            Yii::$service->helper->errors->add("password can not empty");
            
            return null;
        }
T
Terry 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        
        if ($this->_userFormModel[$primaryKey]) {
            if ($this->_userFormModel->validate()) {
                $this->_userFormModel->save();
                $user_id = $this->_userFormModel[$primaryKey];
            } else {
                $errors = $this->_userFormModel->errors;
                Yii::$service->helper->errors->addByModelErrors($errors);
                
                return null;
            }
        } else {
            if ($this->_userFormModel->validate()) {
                $this->_userFormModel->save();
                $user_id = Yii::$app->db->getLastInsertID();
            } else {
                $errors = $this->_userFormModel->errors;
                Yii::$service->helper->errors->addByModelErrors($errors);
                
                return null;
            }
        }
        
        return $user_id;
    }
    
    public function removeUserAndRole($ids) {
        $removeIds = $this->remove($ids);
        if (is_array($removeIds) && !empty($removeIds)) {
            Yii::$service->admin->userRole->deleteByUserIds($removeIds);
            
            return true;
        } else {
            
            return false;
        }
    }
    

    public function remove($ids){
        if (!$ids) {
            Yii::$service->helper->errors->add('remove id is empty');

            return null;
        }
        $removeIds = [];
        if (is_array($ids) && !empty($ids)) {
            foreach ($ids as $id) {
                $model = $this->_model->findOne($id);
                if ($model->username !== 'admin') {
                    $model->delete();
                    $removeIds[] = $id;
                } else {
                    Yii::$service->helper->errors->add('you can not delete admin user');
                }
            }
        } else {
            $id = $ids;
            $model = $this->_model->findOne($id);
            if ($model->username !== 'admin') {
                $model->delete();
                $removeIds[] = $id;
            } else {
                Yii::$service->helper->errors->add('you can not delete admin user');
            }
        }

        return $removeIds;
    }
T
Terry 已提交
239 240
    
    
T
Terry 已提交
241 242 243 244



    /**
T
Terry 已提交
245
     * @param $ids | Int Array
T
Terry 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
     * @return 得到相应用户的数组。
     */
    public function getIdAndNameArrByIds($ids)
    {
        $user_coll = $this->_model->find()
            ->asArray()
            ->select(['id', 'username'])
            ->where([
                'in', 'id', $ids,
            ])->all();
        $users = [];
        foreach ($user_coll as $one) {
            $users[$one['id']] = $one['username'];
        }

        return $users;
    }

}