Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • None
  • system
    • base
    • caching
    • console
    • db
      • ar
      • schema
    • validators
    • web
      • actions
      • auth
      • helpers
      • widgets
        • captcha
        • pagers
  • zii
    • widgets
      • grid

Classes

  • UsersController
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /*****************************************************************************************
  3:  * X2Engine Open Source Edition is a customer relationship management program developed by
  4:  * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  5:  * 
  6:  * This program is free software; you can redistribute it and/or modify it under
  7:  * the terms of the GNU Affero General Public License version 3 as published by the
  8:  * Free Software Foundation with the addition of the following permission added
  9:  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 10:  * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
 11:  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 12:  * 
 13:  * This program is distributed in the hope that it will be useful, but WITHOUT
 14:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 15:  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 16:  * details.
 17:  * 
 18:  * You should have received a copy of the GNU Affero General Public License along with
 19:  * this program; if not, see http://www.gnu.org/licenses or write to the Free
 20:  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 21:  * 02110-1301 USA.
 22:  * 
 23:  * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
 24:  * California 95067, USA. or at email address contact@x2engine.com.
 25:  * 
 26:  * The interactive user interfaces in modified source and object code versions
 27:  * of this program must display Appropriate Legal Notices, as required under
 28:  * Section 5 of the GNU Affero General Public License version 3.
 29:  * 
 30:  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 31:  * these Appropriate Legal Notices must retain the display of the "Powered by
 32:  * X2Engine" logo. If the display of the logo is not reasonably feasible for
 33:  * technical reasons, the Appropriate Legal Notices must display the words
 34:  * "Powered by X2Engine".
 35:  *****************************************************************************************/
 36: 
 37: /**
 38:  * @package application.modules.users.controllers
 39:  */
 40: class UsersController extends x2base {
 41: 
 42:     public $modelClass = 'User';
 43: 
 44: //    public function behaviors() {
 45: //        return array_merge(parent::behaviors(), array(
 46: //            'X2MobileControllerBehavior' => array(
 47: //                'class' => 
 48: //                    'application.modules.mobile.components.behaviors.X2MobileControllerBehavior'
 49: //            ),
 50: //        ));
 51: //    }
 52: 
 53:     /**
 54:      * Specifies the access control rules.
 55:      * This method is used by the 'accessControl' filter.
 56:      * @return array access control rules
 57:      */
 58:     public function accessRules() {
 59:         return array(
 60:             array('allow',
 61:                 'actions'=>array('createAccount'),
 62:                 'users'=>array('*')
 63:             ),
 64:             array('allow',
 65:                 'actions'=>array('addTopContact','removeTopContact'),
 66:                 'users'=>array('@'),
 67:             ),
 68:             array('allow', // allow admin user to perform 'admin' and 'delete' actions
 69:                 'actions'=>array('view','index','create','update','admin','delete','search','inviteUsers'),
 70:                 'users'=>array('admin'),
 71:             ),
 72:             array('deny',  // deny all users
 73:                 'users'=>array('*'),
 74:             ),
 75:         );
 76:     }
 77: 
 78:     public function actionIndex(){
 79:         $this->redirect('admin');
 80:     }
 81: 
 82:     /**
 83:      * Displays a particular model.
 84:      * @param integer $id the ID of the model to be displayed
 85:      */
 86:     public function actionView($id) {
 87:         $user=User::model()->findByPk($id);
 88:         $dataProvider=new CActiveDataProvider('Actions', array(
 89:             'criteria'=>array(
 90:                 'order'=>'complete DESC',
 91:                 'condition'=>'assignedTo=\''.$user->username.'\'',
 92:         )));
 93:         $actionHistory=$dataProvider->getData();
 94:         $this->render('view',array(
 95:             'model'=>$this->loadModel($id),
 96:             'actionHistory'=>$actionHistory,
 97:         ));
 98:     }
 99: 
100:     /**
101:      * Creates a new model.
102:      * If creation is successful, the browser will be redirected to the 'view' page.
103:      */
104:     public function actionCreate() {
105:         $model=new User;
106:         $groups=array();
107:         foreach(Groups::model()->findAll() as $group){
108:             $groups[$group->id]=CHtml::encode($group->name);
109:         }
110:         $roles=array();
111:         foreach(Roles::model()->findAll() as $role){
112:             $roles[$role->id]=CHtml::encode($role->name);
113:         }
114: 
115:         // Uncomment the following line if AJAX validation is needed
116:         // $this->performAjaxValidation($model);
117: 
118:         $unhashedPassword = '';
119:         if(isset($_POST['User'])) {
120:             $model->attributes=$_POST['User'];
121:             //Temporarily maintain unhashed in case of validation error
122:             $unhashedPassword = $model->password;
123:             
124:                 $model->password = PasswordUtil::createHash($model->password);
125:             $model->userKey=substr(str_shuffle(str_repeat(
126:                 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 32)), 0, 32);
127:             $profile=new Profile;
128:             $profile->fullName=$model->firstName." ".$model->lastName;
129:             $profile->username=$model->username;
130:             $profile->allowPost=1;
131:             $profile->emailAddress=$model->emailAddress;
132:             $profile->status=$model->status;
133: 
134:              
135: 
136:             if($model->save()){
137:                 $profile->id=$model->id;
138:                 $profile->save();
139:                 if(isset($_POST['roles'])){
140:                     $roles=$_POST['roles'];
141:                     foreach($roles as $role){
142:                         $link=new RoleToUser;
143:                         $link->roleId=$role;
144:                         $link->userId=$model->id;
145:                         $link->type="user";
146:                         $link->save();
147:                     }
148:                 }
149:                 if(isset($_POST['groups'])){
150:                     $groups=$_POST['groups'];
151:                     foreach($groups as $group){
152:                         $link=new GroupToUser;
153:                         $link->groupId=$group;
154:                         $link->userId=$model->id;
155:                         $link->username=$model->username;
156:                         $link->save();
157:                     }
158:                 }
159:                 $this->redirect(array('view','id'=>$model->id));
160:             }
161:         }
162:         $model->password = $unhashedPassword;
163: 
164:         $this->render('create',array(
165:             'model'=>$model,
166:             'groups'=>$groups,
167:             'roles'=>$roles,
168:             'selectedGroups'=>array(),
169:             'selectedRoles'=>array(),
170:         ));
171:     }
172: 
173:     public function actionCreateAccount(){
174:         Yii::import('application.components.ThemeGenerator.LoginThemeHelper');
175:         $this->layout='//layouts/login';
176:         if(isset($_GET['key'])){
177:             $key=$_GET['key'];
178:             $user=User::model()->findByAttributes(array('inviteKey'=>$key));
179:             if(isset($user)){
180:                 $user->setScenario('insert');
181:                 if($key==$user->inviteKey){
182:                     if(isset($_POST['User'])) {
183:                         $model=$user;
184:                         $model->attributes=$_POST['User'];
185:                         $model->status=1;
186:                         //$this->updateChangelog($model);
187:                         
188:                             $model->password = PasswordUtil::createHash($model->password);
189:                         $model->userKey=substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 32)), 0, 32);
190:                         $profile=new Profile;
191:                         $profile->fullName=$model->firstName." ".$model->lastName;
192:                         $profile->username=$model->username;
193:                         $profile->allowPost=1;
194:                         $profile->emailAddress=$model->emailAddress;
195:                         $profile->status=$model->status;
196: 
197:                         if($model->save()){
198:                             $model->inviteKey=null;
199:                             $model->temporary=0;
200:                             $model->save();
201:                             $profile->id=$model->id;
202:                             $profile->save();
203:                             $this->redirect(array('/site/login'));
204:                         }
205:                     }
206:                     $this->render('createAccount',array(
207:                         'user'=>$user,
208:                     ));
209:                 }else{
210:                     $this->redirect($this->createUrl('/site/login'));
211:                 }
212:             }else{
213:                 $this->redirect($this->createUrl('/site/login'));
214:             }
215:         }else{
216:             $this->redirect($this->createUrl('/site/login'));
217:         }
218:     }
219: 
220:     /**
221:      * Updates a particular model.
222:      * If update is successful, the browser will be redirected to the 'view' page.
223:      * @param integer $id the ID of the model to be updated
224:      */
225:     public function actionUpdate($id) {
226:         $model=$this->loadModel($id);
227:         $groups=array();
228:         foreach(Groups::model()->findAll() as $group){
229:             $groups[$group->id]=CHtml::encode($group->name);
230:         }
231:         $selectedGroups=array();
232:         foreach(GroupToUser::model()->findAllByAttributes(array('userId'=>$model->id)) as $link){
233:             $selectedGroups[]=$link->groupId;
234:         }
235:         $roles=array();
236:         foreach(Roles::model()->findAll() as $role){
237:             $roles[$role->id]=CHtml::encode($role->name);
238:         }
239:         $selectedRoles=array();
240:         foreach(RoleToUser::model()->findAllByAttributes(array('userId'=>$model->id)) as $link){
241:             $selectedRoles[]=$link->roleId;
242:         }
243: 
244:         // Uncomment the following line if AJAX validation is needed
245:         // $this->performAjaxValidation($model);
246: 
247:         if (!isset($model->userAlias))
248:             $model->userAlias = $model->username;
249: 
250:         if(isset($_POST['User'])) {
251:             $old=$model->attributes;
252:             $temp=$model->password;
253:             $model->attributes=$_POST['User'];
254: 
255:             if($model->password!="") {
256:                 
257:                     $model->password = PasswordUtil::createHash($model->password);
258:             } else {
259:                 $model->password=$temp;
260:             }
261:             if(empty($model->userKey)){
262:                 $model->userKey=substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 32)), 0, 32);
263:             }
264:             if($model->save()){
265:                 $profile = $model->profile;
266:                 if(!empty($profile)) {
267:                     $profile->emailAddress = $model->emailAddress;
268:                     $profile->fullName = $model->firstName.' '.$model->lastName;
269:                     $profile->save();
270:                 }
271:                 if($old['username']!=$model->username){
272:                     $fieldRecords=Fields::model()->findAllByAttributes(array('fieldName'=>'assignedTo'));
273:                     $modelList=array();
274:                     foreach($fieldRecords as $record){
275:                         $modelList[$record->modelName]=$record->linkType;
276:                     }
277:                     foreach($modelList as $modelName=>$type){
278:                         if($modelName=='Quotes')
279:                             $modelName="Quote";
280:                         if($modelName=='Products')
281:                             $modelName='Product';
282:                         if(empty($type)){
283:                             $list=X2Model::model($modelName)->findAllByAttributes(array('assignedTo'=>$old['username']));
284:                             foreach($list as $item){
285:                                 $item->assignedTo=$model->username;
286:                                 $item->save();
287:                             }
288:                         }else{
289:                             $list=X2Model::model($modelName)->findAllBySql(
290:                                     "SELECT * FROM ".X2Model::model($modelName)->tableName()
291:                                     ." WHERE assignedTo LIKE '%".$old['username']."%'");
292:                             foreach($list as $item){
293:                                 $assignedTo=explode(", ",$item->assignedTo);
294:                                 $key=array_search($old['username'],$assignedTo);
295:                                 if($key>=0){
296:                                     $assignedTo[$key]=$model->username;
297:                                 }
298:                                 $item->assignedTo=implode(", ",$assignedTo);
299:                                 $item->save();
300:                             }
301:                         }
302:                     }
303: 
304:                     $profile=Profile::model()->findByAttributes(array('username'=>$old['username']));
305:                     if(isset($profile)){
306:                         $profile->username=$model->username;
307:                         $profile->save();
308:                     }
309: 
310:                 }
311:                 foreach(RoleToUser::model()->findAllByAttributes(array('userId'=>$model->id)) as $link){
312:                     $link->delete();
313:                 }
314:                 foreach(GroupToUser::model()->findAllByAttributes(array('userId'=>$model->id)) as $link){
315:                     $link->delete();
316:                 }
317:                 if(isset($_POST['roles'])){
318:                     $roles=$_POST['roles'];
319:                     foreach($roles as $role){
320:                         $link=new RoleToUser;
321:                         $link->roleId=$role;
322:                         $link->type="user";
323:                         $link->userId=$model->id;
324:                         $link->save();
325:                     }
326:                 }
327:                 if(isset($_POST['groups'])){
328:                     $groups=$_POST['groups'];
329:                     foreach($groups as $group){
330:                         $link=new GroupToUser;
331:                         $link->groupId=$group;
332:                         $link->userId=$model->id;
333:                         $link->username=$model->username;
334:                         $link->save();
335:                     }
336:                 }
337:                 $this->redirect(array('view','id'=>$model->id));
338:             }
339:         }
340: 
341:         $this->render('update',array(
342:             'model'=>$model,
343:             'groups'=>$groups,
344:             'roles'=>$roles,
345:             'selectedGroups'=>$selectedGroups,
346:             'selectedRoles'=>$selectedRoles,
347:         ));
348:     }
349: 
350:     public function actionInviteUsers(){
351: 
352:         if(isset($_POST['emails'])){
353:             $list=$_POST['emails'];
354: 
355:             $body="Hello,
356: 
357: You are receiving this email because your X2Engine administrator has invited you to create an account.
358: Please click on the link below to create an account at X2Engine!
359: 
360: ";
361: 
362:             $subject="Create Your X2Engine User Account";
363:             $list=trim($list);
364:             $emails=explode(',',$list);
365:             foreach($emails as &$email){
366:                 $key=substr(str_shuffle(str_repeat(
367:                     'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',16)),0, 16);
368:                 $user=new User('invite');
369:                 $email=trim($email);
370:                 $user->inviteKey=$key;
371:                 $user->temporary=1;
372:                 $user->emailAddress=$email;
373:                 $user->status=0;
374:                 $userList=User::model()->findAllByAttributes(
375:                     array('emailAddress'=>$email,'temporary'=>1));
376:                 foreach($userList as $userRecord){
377:                     if(isset($userRecord)){
378:                         $userRecord->delete();
379:                     }
380:                 }
381:                 $user->save();
382:                 $link=CHtml::link(
383:                     'Create Account',
384:                     (@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . 
385:                     $this->createUrl('/users/users/createAccount',array('key'=>$key)));
386:                 $mail=new InlineEmail;
387:                 $mail->to=$email;
388:                 // Get email password
389:                 $cred = Credentials::model()->getDefaultUserAccount(
390:                     Credentials::$sysUseId['systemResponseEmail'],'email');
391:                 if($cred==Credentials::LEGACY_ID)
392:                     $cred = Credentials::model()->getDefaultUserAccount(
393:                         Yii::app()->user->id,'email');
394:                 if($cred != Credentials::LEGACY_ID)
395:                     $mail->credId = $cred;
396:                 $mail->subject=$subject;
397:                 $mail->message=$body."<br><br>".$link;
398:                 $mail->contactFlag=false;
399:                 if($mail->prepareBody()){
400:                     $mail->deliver();
401:                 }else{
402:                 }
403:             }
404:             $this->redirect('admin');
405:         }
406: 
407:         $this->render('inviteUsers');
408:     }
409: 
410:     public function actionDeleteTemporary(){
411:         $deleted=User::model()->deleteAllByAttributes(array('temporary'=>1));
412:         $this->redirect('admin');
413:     }
414: 
415:     /**
416:      * Manages all models.
417:      */
418:     public function actionAdmin() {
419:         $model=new User('search');
420:         $this->render('admin',array('model'=>$model,'count'=>User::model()->countByAttributes(array('temporary'=>1))));
421:     }
422: 
423:     public function actionDelete($id) {
424:         if($id != 1){
425:             $model=$this->loadModel($id);
426:             if(Yii::app()->request->isPostRequest) {
427:                 $model->delete();
428:             } else {
429:                 throw new CHttpException(
430:                     400,Yii::t('app','Invalid request. Please do not repeat this request again.'));
431:             }
432:             /* if AJAX request (triggered by deletion via admin grid view), we should not redirect 
433:             the browser */
434:             if(!isset($_GET['ajax'])) {
435:                 $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
436:             }
437:         }else{
438:             throw new CHttpException(
439:                 400,Yii::t('app','Cannot delete admin user.  Please do not repeat this request.'));
440:         }
441:     }
442: 
443:     public function actionAddTopContact($recordId, $modelClass) {
444:         Yii::import('application.components.leftWidget.TopContacts');
445:         $model = $this->getModelFromTypeAndId ($modelClass, $recordId, false);
446:         if (TopContacts::addBookmark ($model))
447:             $this->renderTopContacts();
448:     }
449: 
450:     public function actionRemoveTopContact($recordId, $modelClass) {
451:         Yii::import('application.components.leftWidget.TopContacts');
452:         $model = $this->getModelFromTypeAndId ($modelClass, $recordId, false);
453:         if (TopContacts::removeBookmark ($model))
454:             $this->renderTopContacts();
455:     }
456: 
457:     private function renderTopContacts() {
458:         $this->renderPartial('application.components.leftWidget.views.topContacts',array(
459:             'bookmarkRecords'=>User::getTopContacts(),
460:             //'viewId'=>$viewId
461:         ));
462:     }
463: 
464:     /**
465:      * Create a menu for Users
466:      * @param array Menu options to remove
467:      * @param X2Model Model object passed to the view
468:      * @param array Additional menu parameters
469:      */
470:     public function insertMenu($selectOptions = array(), $model = null, $menuParams = null) {
471:         $Users = Modules::displayName();
472:         $User = Modules::displayName(false);
473:         $modelId = isset($model) ? $model->id : 0;
474: 
475:         /**
476:          * To show all options:
477:          * $menuOptions = array(
478:          *     'feed', 'admin', 'create', 'invite', 'view', 'profile', 'edit', 'delete',
479:          * );
480:          */
481: 
482:         $menuItems = array(
483:             array(
484:                 'name'=>'feed',
485:                 'label'=>Yii::t('profile','Social Feed'),
486:                 'url'=>array('/profile/index')
487:             ),
488:             array(
489:                 'name'=>'admin',
490:                 'label' => Yii::t('users', 'Manage {users}', array(
491:                     '{users}' => $Users,
492:                 )),
493:                 'url'=>array('admin')
494:             ),
495:             array(
496:                 'name'=>'create',
497:                 'label' => Yii::t('users', 'Create {user}', array(
498:                     '{user}' => $User,
499:                 )),
500:                 'url' => array('create')
501:             ),
502:             array(
503:                 'name'=>'invite',
504:                 'label' => Yii::t('users', 'Invite {users}', array(
505:                     '{users}' => $Users,
506:                 )),
507:                 'url' => array('inviteUsers')
508:             ),
509:             array(
510:                 'name'=>'view',
511:                 'label'=>Yii::t('users','View {user}', array(
512:                     '{user}' => $User,
513:                 )),
514:                 'url'=>array('view', 'id'=>$modelId)
515:             ),
516:             array(
517:                 'name'=>'profile',
518:                 'label'=>Yii::t('profile','View Profile'),
519:                 'url'=>array('/profile/view','id'=>$modelId)
520:             ),
521:             array(
522:                 'name'=>'edit',
523:                 'label'=>Yii::t('users','Update {user}', array(
524:                     '{user}' => $User,
525:                 )),
526:                 'url'=>array('update', 'id'=>$modelId)
527:             ),
528:             array(
529:                 'name'=>'delete',
530:                 'label'=>Yii::t('users','Delete {user}', array(
531:                     '{user}' => $User,
532:                 )),
533:                 'url'=>'#',
534:                 'linkOptions'=>array(
535:                     'submit'=>array('delete','id'=>$modelId),
536:                     'confirm'=>Yii::t('app','Are you sure you want to delete this item?'))
537:             ),
538:         );
539: 
540:         $this->prepareMenu($menuItems, $selectOptions);
541:         $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
542:     }
543: 
544: 
545: }
546: 
X2CRM Documentation API documentation generated by ApiGen 2.8.0