PageRenderTime 77ms CodeModel.GetById 30ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Model/User/User.php

https://github.com/polokk/tudu-web-1
PHP | 693 lines | 483 code | 101 blank | 109 comment | 115 complexity | afbb42bc1d9150caf5c571e1b7e24579 MD5 | raw file
  1<?php
  2/**
  3 * Model User User
  4 *
  5 * LICENSE
  6 *
  7 *
  8 * @category   Model
  9 * @package    Model_User
 10 * @author     Oray-Yongfa
 11 * @copyright  Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
 12 * @link       http://www.tudu.com/
 13 * @version    $Id: User.php 2825 2013-04-15 09:55:11Z chenyongfa $
 14 */
 15
 16/**
 17 * @see Tudu_Dao_Manager
 18 */
 19require_once 'Tudu/Dao/Manager.php';
 20
 21/**
 22 * @see Model_Abstract
 23 */
 24require_once 'Model/Abstract.php';
 25
 26/**
 27 * @category   Model
 28 * @package    Model_User
 29 * @copyright  Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
 30 * @link       http://www.tudu.com/
 31 */
 32class Model_User_User extends Model_Abstract
 33{
 34    const CODE_INVALID_ORGID      = 101;
 35    const CODE_INVALID_UID        = 102;
 36    const CODE_USER_NOTEXISTS     = 103;
 37    const CODE_SAVE_FAILED        = 104;
 38    const CODE_TOO_MUCH_USER      = 105;
 39    const CODE_MISSING_UID        = 106;
 40    const CODE_INVALID_PWD        = 107;
 41    const CODE_INVALID_USERNAME   = 108;
 42    const CODE_INVALID_DOMAINID   = 109;
 43    const CODE_USER_EXISTS        = 110;
 44    const CODE_NOT_ENOUGH_NDSPACE = 111;
 45    const CODE_INVALID_BIRTHDAY   = 112;
 46    const CODE_INVALID_DEPTNAME   = 113;
 47    const CODE_DEPT_PARENT_NOTEXISTS = 114;
 48    const CODE_DEPT_EXISTS        = 115;
 49    const CODE_SAVE_DEPT_FAILED   = 116;
 50    const CODE_MISSING_UNID       = 117;
 51    const CODE_LESS_NDSPACE       = 118;
 52    const CODE_NOT_MODIFY_PWD     = 119;
 53    const CODE_DELETE_SUPER_ADMIN = 120;
 54    const CODE_INVALID_NDQUOTA    = 121;
 55    const CODE_INVALID_EMAIL      = 122;
 56    const CODE_EXCEED_MAX_NDSPACE = 123;
 57
 58    /**
 59     * 创建用户
 60     */
 61    public function create(array $params)
 62    {
 63        $isImport = !empty($params['import']) ? true : false;
 64
 65        // 组织ID必须有
 66        if (empty($params['orgid'])) {
 67            require_once 'Model/User/Exception.php';
 68            throw new Model_User_Exception('Missing or invalid value of parameter "orgid"', self::CODE_INVALID_ORGID);
 69        }
 70        $orgId = $params['orgid'];
 71
 72        /* @var $daoOrg Dao_Md_Org_Org */
 73        $daoOrg = Tudu_Dao_Manager::getDao('Dao_Md_Org_Org', Tudu_Dao_Manager::DB_MD);
 74        /* @var $daoUser Dao_Md_User_User */
 75        $daoUser = Tudu_Dao_Manager::getDao('Dao_Md_User_User', Tudu_Dao_Manager::DB_MD);
 76        if ($isImport) {
 77            if (!empty($params['exist-parentid'])) {
 78                require_once 'Model/User/Exception.php';
 79                throw new Model_User_Exception('Parent dept is not exists', self::CODE_DEPT_PARENT_NOTEXISTS);
 80            }
 81            if (!empty($params['deptid']) && $params['deptid'] == '^new') {
 82                require_once 'Model/User/Exception.php';
 83                throw new Model_User_Exception('Create dept failed', self::CODE_SAVE_DEPT_FAILED);
 84            }
 85        }
 86
 87        // 读取组织信息
 88        $org = $daoOrg->getOrg(array('orgid' => $orgId));
 89        if ($isImport) {
 90            $params['password'] = $org->defaultPassword;
 91        }
 92
 93        // 组织用户是否已达到上限
 94        if ($org->maxUsers && $org->maxUsers <= $daoOrg->getUserCount($orgId)) {
 95            require_once 'Model/User/Exception.php';
 96            throw new Model_User_Exception('This organization is too much users', self::CODE_TOO_MUCH_USER);
 97        }
 98
 99        // 用户名
100        if (empty($params['userid'])) {
101            require_once 'Model/User/Exception.php';
102            throw new Model_User_Exception('Missing the value of parameter "userid"', self::CODE_MISSING_UID);
103        }
104        $userId   = $params['userid'];
105        $address  = $userId . '@' . $orgId;
106        $uniqueId = Dao_Md_User_User::getUniqueId($orgId, $userId);
107
108        // 用户名格式验证
109        if (!Oray_Function::isDomainStr($userId)) {
110            require_once 'Model/User/Exception.php';
111            throw new Model_User_Exception('Invalid value of parameter "userid"', self::CODE_INVALID_UID);
112        }
113
114        // 密码
115        if (empty($params['password'])) {
116            require_once 'Model/User/Exception.php';
117            throw new Model_User_Exception('Missing or invalid value of parameter "password"', self::CODE_INVALID_PWD);
118        }
119
120        // 用户真实姓名
121        if (empty($params['truename'])) {
122            require_once 'Model/User/Exception.php';
123            throw new Model_User_Exception('Missing or invalid value of parameter "truename"', self::CODE_INVALID_USERNAME);
124        }
125
126        // 邮箱格式有误
127        if (!empty($params['email']) && !Oray_Function::isEmail($params['email'])) {
128            require_once 'Model/User/Exception.php';
129            throw new Model_User_Exception('Invalid value of parameter "email"', self::CODE_INVALID_EMAIL);
130        }
131
132        // 用户已存在
133        if ($daoUser->existsUser($orgId, $userId)) {
134            require_once 'Model/User/Exception.php';
135            throw new Model_User_Exception('User is exists in this organization', self::CODE_USER_EXISTS);
136        }
137
138        // 准备用户参数
139        $user = array(
140            'orgid'          => $orgId,
141            'userid'         => $userId,
142            'uniqueid'       => $uniqueId,
143            'status'         => isset($params['status']) ? (int) $params['status'] : 1,
144            'deptid'         => isset($params['deptid']) ? $params['deptid'] : null,
145            'isshow'         => !empty($params['isshow']) ? 1 : 0,
146            'ordernum'       => isset($params['ordernum']) ? (int) $params['ordernum'] : 0,
147            'initpassword'   => 1,
148            'lastupdatetime' => time()
149        );
150        $userInfo = array(
151            'orgid'    => $orgId,
152            'userid'   => $userId,
153            'truename' => $params['truename'],
154            'password' => $params['password'],
155            'gender'   => (int) $params['gender']
156        );
157        if (isset($params['position'])) {
158            $userInfo['position'] = $params['position'];
159        }
160        if (isset($params['tel'])) {
161            $userInfo['tel']      = $params['tel'];
162        }
163        if (isset($params['mobile'])) {
164            $userInfo['mobile']   = $params['mobile'];
165        }
166        if (isset($params['email'])) {
167            $userInfo['email']   = $params['email'];
168        }
169
170        // 网盘空间
171        if (!empty($params['maxndquota'])) {
172            if ($params['maxndquota'] > 1000) {
173                require_once 'Model/User/Exception.php';
174                throw new Model_User_Exception('You can not set exceed 1000MB netdisk space', self::CODE_EXCEED_MAX_NDSPACE);
175            }
176            $user['maxndquota'] = (float) $params['maxndquota'] * 1000000;
177        }
178
179        // 无效的出生日期
180        if (!empty($params['birthday'])) {
181            if (false === $params['birthday']) {
182                require_once 'Model/User/Exception.php';
183                throw new Model_User_Exception('Missing or invalid value of parameter "birthday"', self::CODE_INVALID_BIRTHDAY);
184            }
185
186            $userInfo['birthday'] = $params['birthday'];
187        }
188
189        if (!Oray_Function::isByte($params['truename'])) {
190            require_once 'Tudu/Pinyin.php';
191            $userInfo['pinyin'] = Tudu_Pinyin::parse($params['truename'], true);
192        }
193
194        if (!empty($params['idnumber'])) {
195            $userInfo['idnumber'] = $params['idnumber'];
196        }
197
198        if (!empty($params['nick'])) {
199            $userInfo['nick'] = $params['nick'];
200        }
201
202        // 创建用户
203        if (!$daoUser->createUser($user)) {
204            require_once 'Model/User/Exception.php';
205            throw new Model_User_Exception('Create user failed', self::CODE_SAVE_FAILED);
206        }
207
208        // 创建用户数据
209        if (!$daoUser->createUserInfo($userInfo)) {
210            require_once 'Model/User/Exception.php';
211            throw new Model_User_Exception('Create user info failed', self::CODE_SAVE_FAILED);
212        }
213
214        // 群组
215        /* @var $daoGroup Dao_Md_User_Group */
216        $daoGroup = Tudu_Dao_Manager::getDao('Dao_Md_User_Group', Tudu_Dao_Manager::DB_MD);
217        $groups   = !empty($params['groupid']) ? $params['groupid'] : array();
218        foreach ($groups as $groupId) {
219            $daoGroup->addUser($orgId, $groupId, $userId);
220        }
221
222        /* @var $daoRole Dao_Md_User_Role */
223        $daoRole = Tudu_Dao_Manager::getDao('Dao_Md_User_Role', Tudu_Dao_Manager::DB_MD);
224        $roles   = !empty($params['roleid']) ? $params['roleid'] : array();
225        // 权限组
226        foreach ($roles as $roleId) {
227            $daoRole->addUsers($orgId, $roleId, $userId);
228        }
229
230        /* @var $daoCast Dao_Md_User_Cast */
231        $daoCast   = Tudu_Dao_Manager::getDao('Dao_Md_User_Cast', Tudu_Dao_Manager::DB_MD);
232        $castDepts = !empty($params['castdept']) ? $params['castdept'] : array();
233        $castUsers = !empty($params['castuser']) ? $params['castuser'] : array();
234
235        // 添加不可见部门
236        foreach ($castDepts as $deptId) {
237            if (!trim($deptId) || $deptId == '^root') {
238                continue ;
239            }
240            $daoCast->hideDepartment($orgId, $userId, $deptId);
241        }
242
243        // 添加不可见用户
244        foreach ($castUsers as $uId) {
245            if (!trim($uId) || $uId == $userId) {
246                continue ;
247            }
248            $daoCast->hideUser($orgId, $userId, $uId);
249        }
250        $daoCast->updateDepartment($orgId, $userId, $user['deptid']);
251
252        // 修改企业默认密码
253        if ($org->defaultPassword != $params['password']) {
254            $daoOrg->updateOrg($orgId, array('defaultpassword' => $params['password']));
255        }
256
257        // 发送通知,插入消息队列
258        if (Tudu_Model::hasResource(Tudu_Model::RESOURCE_CONFIG)) {
259            $config = Tudu_Model::getResource(Tudu_Model::RESOURCE_CONFIG);
260
261            if ($config['httpsqs']) {
262                $options = $config['httpsqs'];
263                $httpsqs = new Oray_Httpsqs($options['host'], $options['port'], $options['charset'], $options['name']);
264
265                $data = implode(' ', array(
266                    Dao_Md_Log_Oplog::MODULE_DEPT,
267                    Dao_Md_Log_Oplog::OPERATION_CREATE,
268                    'user',
269                    implode(':', array($orgId, $address, $uniqueId, $params['truename']))
270                ));
271
272                $httpsqs->put($data);
273            }
274        }
275
276        // 添加后台操作日志
277        if (!empty($params['operator']) && !empty($params['clientip'])) {
278            $params['local'] = empty($params['local']) ? null : $params['local'];
279            $this->_createLog(
280                Dao_Md_Log_Oplog::MODULE_USER,
281                Dao_Md_Log_Oplog::OPERATION_CREATE,
282                null,
283                array('orgid' => $orgId, 'operator' => $params['operator'], 'clientip' => $params['clientip'], 'local' => $params['local']),
284                implode(':', array($orgId, $address, $uniqueId)),
285                array('truename' => $params['truename'], 'account' => $address)
286            );
287        }
288    }
289
290    /**
291     * 更新用户
292     */
293    public function update(array $params)
294    {
295        // 组织ID必须有
296        if (empty($params['orgid'])) {
297            require_once 'Model/User/Exception.php';
298            throw new Model_User_Exception('Missing or invalid value of parameter "orgid"', self::CODE_INVALID_ORGID);
299        }
300        $orgId = $params['orgid'];
301
302        $edit = array(
303            'truename' => true,
304            'password' => true,
305            'dept'     => true,
306            'status'   => true,
307            'role'     => true,
308            'group'    => true,
309            'cast'     => true,
310            'netdisk'  => true,
311            'email'    => true
312        );
313
314        if (!empty($params['edit'])) {
315            $edit = $params['edit'];
316        }
317
318        /* @var $daoUser Dao_Md_User_User */
319        $daoUser = Tudu_Dao_Manager::getDao('Dao_Md_User_User', Tudu_Dao_Manager::DB_MD);
320
321        // 用户名
322        if (empty($params['userid'])) {
323            require_once 'Model/User/Exception.php';
324            throw new Model_User_Exception('Missing the value of parameter "userid"', self::CODE_MISSING_UID);
325        }
326
327        $userId = $params['userid'];
328        $user   = $daoUser->getUser(array('orgid' => $orgId, 'userid' => $userId));
329        $userIf = $daoUser->getUserInfo(array('orgid' => $orgId, 'userid' => $userId));
330
331        // 用户不存在
332        if (null === $user) {
333            require_once 'Model/User/Exception.php';
334            throw new Model_User_Exception('Missing the value of parameter "userid"', self::CODE_USER_NOTEXISTS);
335        }
336
337        // 用户真实姓名
338        if ($edit['truename'] && empty($params['truename'])) {
339            require_once 'Model/User/Exception.php';
340            throw new Model_User_Exception('Missing or invalid value of parameter "truename"', self::CODE_INVALID_USERNAME);
341        }
342
343        // 邮箱格式有误
344        if ($edit['email'] && !empty($params['email']) && !Oray_Function::isEmail($params['email'])) {
345            require_once 'Model/User/Exception.php';
346            throw new Model_User_Exception('Invalid value of parameter "email"', self::CODE_INVALID_EMAIL);
347        }
348
349        $userParam = array();
350        $userInfo  = array();
351
352        if ($edit['status']) {
353            $userParam['status'] = isset($params['status']) ? (int) $params['status'] : 1;
354        }
355
356        if ($edit['dept']) {
357            $userParam['deptid'] = isset($params['deptid']) ? $params['deptid'] : null;
358        }
359
360        if (isset($params['ordernum'])) {
361            $userParam['ordernum'] = (int) $params['ordernum'];
362        }
363
364        $userParam['lastupdatetime'] = time();
365
366        if (isset($params['isshow'])) {
367            $userParam['isshow'] = $params['isshow'];
368        }
369
370        if (isset($params['truename'])) {
371            $userInfo['truename'] = $params['truename'];
372        }
373        if (isset($params['position'])) {
374            $userInfo['position'] = $params['position'];
375        }
376        if (isset($params['gender'])) {
377            $userInfo['gender']   = (int) $params['gender'];
378        }
379        if (isset($params['tel'])) {
380            $userInfo['tel']      = $params['tel'];
381        }
382        if (isset($params['mobile'])) {
383            $userInfo['mobile']   = $params['mobile'];
384        }
385        if (isset($params['email'])) {
386            $userInfo['email']   = $params['email'];
387        }
388
389        /* @var $daoOrg Dao_Md_Org_Org */
390        $daoOrg = Tudu_Dao_Manager::getDao('Dao_Md_Org_Org', Tudu_Dao_Manager::DB_MD);
391
392        // 读取组织信息
393        $org = $daoOrg->getOrg(array('orgid' => $orgId));
394
395        // 网盘空间
396        if ($edit['netdisk'] && !empty($params['maxndquota'])) {
397            $ndQuota = (float) $params['maxndquota'] * 1000000;
398            if ($ndQuota != $user->maxNdQuota) {
399                if ($params['maxndquota'] > 1000) {
400                    require_once 'Model/User/Exception.php';
401                    throw new Model_User_Exception('You can not set exceed 1000MB netdisk space', self::CODE_EXCEED_MAX_NDSPACE);
402                }
403
404                /* @var $daoFolder Dao_Td_Netdisk_Folder */
405                $daoFolder = Tudu_Dao_Manager::getDao('Dao_Td_Netdisk_Folder', Tudu_Dao_Manager::DB_TS);
406
407                $folderRoot = $daoFolder->getFolder(array('uniqueid' => $user->uniqueId, 'folderid' => '^root'));
408                if (null !== $folderRoot && $ndQuota < $folderRoot->folderSize) {
409                    require_once 'Model/User/Exception.php';
410                    throw new Model_User_Exception('This netdisk space can not less than the used netdisk space', self::CODE_LESS_NDSPACE);
411                }
412
413                // 更新用户网盘跟文件夹空间
414                if (null !== $folderRoot) {
415                    $daoFolder->updateFolder($user->uniqueId, '^root', array('maxquota' => $ndQuota));
416                }
417
418                $userParam['maxndquota'] = $ndQuota;
419            }
420        }
421
422        // 无效的出生日期
423        if (!empty($params['birthday'])) {
424            if (false === $params['birthday']) {
425                require_once 'Model/User/Exception.php';
426                throw new Model_User_Exception('Missing or invalid value of parameter "birthday"', self::CODE_INVALID_BIRTHDAY);
427            }
428
429            $userInfo['birthday'] = $params['birthday'];
430        }
431
432        if ($edit['truename'] && !Oray_Function::isByte($params['truename'])) {
433            require_once 'Tudu/Pinyin.php';
434            $userInfo['pinyin'] = Tudu_Pinyin::parse($params['truename'], true);
435        }
436
437        if (!empty($params['nick'])) {
438            $userInfo['nick'] = $params['nick'];
439        }
440
441        if (!empty($params['idnumber'])) {
442            $userInfo['idnumber'] = $params['idnumber'];
443        }
444
445        if ($edit['password'] && !empty($params['password'])) {
446            if ($daoUser->isAdmin($orgId, $userId)) {
447                require_once 'Model/User/Exception.php';
448                throw new Model_User_Exception('Can not modify the administrator password', self::CODE_NOT_MODIFY_PWD);
449            }
450
451            $userParam['initpassword'] = 1;
452            $userInfo['password'] = $params['password'];
453        }
454
455        // 用户头像
456        if (!empty($params['avatars'])) {
457            $userInfo['avatartype'] = $params['avatartype'];
458            $userInfo['avatars']    = $params['avatars'];
459
460            /* @var $daoImContact Dao_Im_Contact_Contact */
461            //$daoImContact = Tudu_Dao_Manager::getDao('Dao_Im_Contact_Contact', Tudu_Dao_Manager::DB_IM);
462
463            // 需要更新im自定义联系人表的updatetime
464            // im通过更新时间判断是否需要获取用户头像
465            //$daoImContact->updateUser($userId . '@' . $orgId, array('updatetime' => time()));
466        }
467
468        // 更新用户数据
469        if (!empty($userParam)) {
470            if (!$daoUser->updateUser($orgId, $user->userId, $userParam)) {
471                require_once 'Model/User/Exception.php';
472                throw new Model_User_Exception('Update user failed', self::CODE_SAVE_FAILED);
473            }
474        }
475
476        if (!empty($userInfo)) {
477            if (!$daoUser->updateUserInfo($orgId, $user->userId, $userInfo)) {
478                require_once 'Model/User/Exception.php';
479                throw new Model_User_Exception('Update user info failed', self::CODE_SAVE_FAILED);
480            }
481        }
482
483        // 群组
484        /* @var $daoGroup Dao_Md_User_Group */
485        $daoGroup = Tudu_Dao_Manager::getDao('Dao_Md_User_Group', Tudu_Dao_Manager::DB_MD);
486        if ($edit['group']) {
487            $groups = !empty($params['groupid']) ? $params['groupid'] : array();
488
489            $daoUser->removeGroups($user->orgId, $user->userId);
490            foreach ($groups as $groupId) {
491                $daoGroup->addUser($orgId, $groupId, $userId);
492            }
493        }
494
495        // 权限组
496        /* @var $daoRole Dao_Md_User_Role */
497        $daoRole = Tudu_Dao_Manager::getDao('Dao_Md_User_Role', Tudu_Dao_Manager::DB_MD);
498        if ($edit['role']) {
499            $roles = !empty($params['roleid']) ? $params['roleid'] : array();
500
501            $daoUser->removeRoles($user->orgId, $user->userId);
502            foreach ($roles as $roleId) {
503                $daoRole->addUsers($orgId, $roleId, $userId);
504            }
505        }
506
507        // 组织架构
508        /* @var $daoCast Dao_Md_User_Cast */
509        $daoCast   = Tudu_Dao_Manager::getDao('Dao_Md_User_Cast', Tudu_Dao_Manager::DB_MD);
510        if ($edit['cast']) {
511            $castDepts = !empty($params['castdept']) ? $params['castdept'] : array();
512            $castUsers = !empty($params['castuser']) ? $params['castuser'] : array();
513
514            // 清除组织架构
515            $daoCast->clear($orgId, $userId);
516            // 隐藏部门
517            foreach ($castDepts as $dept) {
518                if (!trim($dept) || $dept == '^root' || $dept == $userParam['deptid']) {
519                    continue ;
520                }
521                $daoCast->hideDepartment($orgId, $userId, $dept);
522            }
523
524            // 隐藏用户
525            foreach ($castUsers as $uId) {
526                if (!$uId || $uId == $userId) {
527                    continue ;
528                }
529                $daoCast->hideUser($orgId, $userId, $uId);
530            }
531
532            // 更换部门
533            if ($user->deptId != $userParam['deptid']) {
534                $daoCast->updateDepartment($orgId, $userId, $userParam['deptid']);
535            }
536        }
537
538        // 修改企业默认密码
539        if ($edit['password'] && !empty($params['password']) && $org->defaultPassword != $params['password']) {
540            $daoOrg->updateOrg($orgId, array('defaultpassword' => $params['password']));
541        }
542
543        // 发送通知,插入消息队列
544        if (Tudu_Model::hasResource(Tudu_Model::RESOURCE_CONFIG)) {
545            $config = Tudu_Model::getResource(Tudu_Model::RESOURCE_CONFIG);
546
547            if ($config['httpsqs']) {
548                $options = $config['httpsqs'];
549                $httpsqs = new Oray_Httpsqs($options['host'], $options['port'], $options['charset'], $options['name']);
550
551                $data = implode(' ', array(
552                    Dao_Md_Log_Oplog::MODULE_DEPT,
553                    Dao_Md_Log_Oplog::OPERATION_UPDATE,
554                    null,
555                    implode(':', array($orgId, $user->userName, $user->uniqueId, ''))
556                ));
557
558                $httpsqs->put($data);
559            }
560        }
561
562        // 添加后台操作日志
563        if (!empty($params['operator']) && !empty($params['clientip'])) {
564            $params['local'] = empty($params['local']) ? null : $params['local'];
565            $trueName = $edit['truename'] ? $params['truename'] : $userIf->trueName;
566            $this->_createLog(
567                Dao_Md_Log_Oplog::MODULE_USER,
568                Dao_Md_Log_Oplog::OPERATION_UPDATE,
569                null,
570                array('orgid' => $orgId, 'operator' => $params['operator'], 'clientip' => $params['clientip'], 'local' => $params['local']),
571                implode(':', array($orgId, $user->userName, $user->uniqueId)),
572                array('truename' => $trueName, 'account' => $user->userName)
573            );
574        }
575    }
576
577    /**
578     * 删除用户
579     */
580    public function delete(array $params)
581    {
582        // 组织ID必须有
583        if (empty($params['orgid'])) {
584            require_once 'Model/User/Exception.php';
585            throw new Model_User_Exception('Missing or invalid value of parameter "orgid"', self::CODE_INVALID_ORGID);
586        }
587        $orgId = $params['orgid'];
588
589        /* @var $daoUser Dao_Md_User_User */
590        $daoUser = Tudu_Dao_Manager::getDao('Dao_Md_User_User', Tudu_Dao_Manager::DB_MD);
591
592        // 用户名
593        if (empty($params['userid'])) {
594            require_once 'Model/User/Exception.php';
595            throw new Model_User_Exception('Missing the value of parameter "userid"', self::CODE_MISSING_UID);
596        }
597
598        $userIds = is_array($params['userid']) ? $params['userid'] : (array) $params['userid'];
599        $ret     = true;
600        $uniqueIds = array();
601
602        foreach ($userIds as $userId) {
603            $cuser    = $daoUser->getUser(array('orgid' => $orgId, 'userid' => $userId));
604            $infouser = $daoUser->getUserInfo(array('orgid' => $orgId, 'userid' => $userId));
605
606            // 用户已不存在
607            if (null == $cuser) {
608                continue;
609            }
610
611            // 是否超级管理员
612            if ($daoUser->isAdmin($orgId, $userId)) {
613                require_once 'Model/User/Exception.php';
614                throw new Model_User_Exception('Can not delete super administrator "'.$userId.'@'.$orgId.'"', self::CODE_DELETE_SUPER_ADMIN);
615            }
616
617            if (!$daoUser->deleteUser($orgId, $userId)) {
618                $ret = false;
619                continue;
620            }
621
622            $uniqueIds[] = $cuser->uniqueId;
623
624            // 添加操作日志
625            if (!empty($params['operator']) && !empty($params['clientip'])) {
626                $params['local'] = empty($params['local']) ? null : $params['local'];
627                $this->_createLog(
628                    Dao_Md_Log_Oplog::MODULE_USER,
629                    Dao_Md_Log_Oplog::OPERATION_DELETE,
630                    null,
631                    array('orgid' => $orgId, 'operator' => $params['operator'], 'clientip' => $params['clientip'], 'local' => $params['local']),
632                    implode(':', array($cuser->orgId, $cuser->userName, $cuser->uniqueId)),
633                    array('truename' => $infouser->trueName, 'account' => $cuser->userName)
634                );
635            }
636        }
637
638        if (!$ret) {
639            require_once 'Model/User/Exception.php';
640            throw new Model_User_Exception('Delete user failed', self::CODE_SAVE_FAILED);
641        }
642
643        // 发送通知,插入消息队列
644        if (Tudu_Model::hasResource(Tudu_Model::RESOURCE_CONFIG)) {
645            $config = Tudu_Model::getResource(Tudu_Model::RESOURCE_CONFIG);
646
647            if ($config['httpsqs']) {
648                $options = $config['httpsqs'];
649                $httpsqs = new Oray_Httpsqs($options['host'], $options['port'], $options['charset'], $options['name']);
650
651                $data = implode(' ', array(
652                    Dao_Md_Log_Oplog::MODULE_DEPT,
653                    Dao_Md_Log_Oplog::OPERATION_DELETE,
654                    null,
655                    implode(':', array($orgId, implode(',', $uniqueIds)))
656                ));
657
658                $httpsqs->put($data);
659            }
660        }
661    }
662
663    /**
664     * 创建管理日志
665     *
666     * @param string $module
667     * @param string $action
668     * @param string $subAction
669     * @param string $description
670     * @return int
671     */
672     protected function _createLog($module, $action, $subAction = null, $params = null, $target = null, array $detail = null)
673     {
674         if (null !== $detail) {
675            $detail = serialize($detail);
676         }
677
678         /* @var $daoLog Dao_Md_Log_Oplog */
679         $daoLog = Tudu_Dao_Manager::getDao('Dao_Md_Log_Oplog', Tudu_Dao_Manager::DB_MD);
680
681         $ret = $daoLog->createAdminLog(array(
682             'orgid'     => $params['orgid'],
683             'userid'    => $params['operator'],
684             'ip'        => $params['clientip'],
685             'module'    => $module,
686             'action'    => $action,
687             'subaction' => $subAction,
688             'target'    => $target,
689             'local'     => $params['local'],
690             'detail'    => $detail
691         ));
692     }
693}