-
Notifications
You must be signed in to change notification settings - Fork 2
/
UserController.php
125 lines (112 loc) · 3.11 KB
/
UserController.php
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
<?php
/**
* This represents the user actions
*/
class UserController extends Controller
{
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array(
'deny',
'actions' => array('admin'),
'users' => array('?'),
'expression' => '$user->isAdmin()' // Does this actually work, adding both users and expression??
),
array('allow', // allow all users to perform 'index' and 'view' actions
'users'=>array('*'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionIndex(){
$model=new User('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['User']))
$model->attributes=$_GET['User'];
$this->render('index',array('model'=>$model));
}
/**
* This creates our new user and also logs them in if the creation is successfull
*/
public function actionCreate(){
$model=new User;
if(isset($_POST['User'])){
$model->attributes=$_POST['User'];
if($model->validate()&&$model->save()){
$identity=new UserIdentity($model->username,'');
$identity->setId($model->_id); // we set the id of the identity to the _id of the user
$identity->errorCode=UserIdentity::ERROR_NONE;
if(Yii::app()->user->login($identity,0)){
$this->redirect('site/index');
}
}
}
$this->render('create',array(
'model'=>$model
));
}
public function actionEdit(){
$model=User::model()->findOne(array('_id'=>Yii::app()->user->id));
if($model===null)
throw new CHttpException(403, 'You are not logged in');
if($file=EMongoFile::populate($model,'avatar')){
if($oldFile=EMongoFile::model()->findOne(array('userId'=>Yii::app()->user->id)))
$oldFile->delete();
$file->userId=$model->_id;
if($file->save()){
Yii::app()->user->setFlash('success', "Avatar Changed!");
}
}
if(isset($_POST['User'])){
$model->attributes=$_POST['User'];
if($model->validate()){
Yii::app()->user->setFlash('success', 'Edit Saved');
return $this->redirect('user/edit');
}
}
$this->render('edit',array(
'model' => $model
));
}
/**
* This lists and allows us to search across all users. Only accessible to Admins
*/
public function actionAdmin()
{
$model=new User('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['User']))
$model->attributes=$_GET['User'];
$this->render('admin',array(
'model'=>$model,
));
}
/**
* Displays the users profile and shows their articles and comments and counts etc
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$model = User::model()->findBy_id($id);
$this->render('view',array(
'model'=>$model,
));
}
}