1: <?php
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: 33: 34: 35:
36:
37: 38: 39:
40: class UsersController extends x2base {
41:
42: public $modelClass = 'User';
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53: 54: 55: 56: 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',
69: 'actions'=>array('view','index','create','update','admin','delete','search','inviteUsers'),
70: 'users'=>array('admin'),
71: ),
72: array('deny',
73: 'users'=>array('*'),
74: ),
75: );
76: }
77:
78: public function actionIndex(){
79: $this->redirect('admin');
80: }
81:
82: 83: 84: 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: 102: 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:
116:
117:
118: $unhashedPassword = '';
119: if(isset($_POST['User'])) {
120: $model->attributes=$_POST['User'];
121:
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:
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: 222: 223: 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:
245:
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:
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: 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: 433:
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:
461: ));
462: }
463:
464: 465: 466: 467: 468: 469:
470: public function ($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: 477: 478: 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: