ProfileController::actionEdit()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 0
1
<?php
2
3
class ProfileController extends CiiController
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
	/**
7
	 * The layout to use for this controller
8
	 * @var string
9
	 */
10
	public $layout = '//layouts/main';
11
12
	/**
13
	 * @return array action filters
14
	 */
15
	public function filters()
16
	{
17
		return CMap::mergeArray(parent::filters(), array('accessControl'));
18
	}
19
20
	/**
21
	 * Specifies the access control rules.
22
	 * This method is used by the 'accessControl' filter.
23
	 * @return array access control rules
24
	 */
25
	public function accessRules()
26
	{
27
		return array(
28
			array('allow',  // Allow all users to any section
29
				'actions' => array('index'),
30
				'users'=>array('*'),
31
			),
32
			array('allow',  // deny all users
33
				'actions' => array('edit', 'resend'),
34
				'users'=>array('@'),
35
			),
36
			array('deny',  // deny all users
37
				'users'=>array('*'),
38
			),
39
		);
40
	}
41
42
	/**
43
	 * Provides functionality to view a given profile
44
	 * @param  int 	  $id          The ID belonging to the user
45
	 * @param  string $username    The user's display name. This isn't super necessary, it just is better for SEO
46
	 */
47
	public function actionIndex($id=NULL, $username=NULL)
48
	{
49
		// If an ID isn't provided, throw an error
50
		if ($id === NULL)
51
			throw new CHttpException(404, Yii::t('ciims.controllers.Profile', "Oops! That user doesn't exist on our network!"));
52
53
		// For SEO, if the display name isn't in the url, reroute it
54
		if ($id !== NULL && $username === NULL)
55
		{
56
			$model = Users::model()->findByPk($id);
57
			if ($model === NULL || $model->status == 0)
58
				throw new CHttpException(404, Yii::t('ciims.controllers.Profile', "Oops! That user doesn't exist on our network!"));
59
			else
60
				$this->redirect('/profile/' . $model->id . '/' . preg_replace('/[^\da-z]/i', '', $model->username));
61
		}
62
63
		$model = Users::model()->findByPk($id);
64
65
		// Don't allow null signings or invalidated users to pollute our site
66
		if($model->status == 0)
67
			throw new CHttpException(404, Yii::t('ciims.controllers.Profile', "Oops! That user doesn't exist on our network!"));
68
69
		$this->pageTitle = Yii::t('ciims.controllers.Profile', 'User {{user}} - CiiMS | {{sitename}}', array('{{user}}' => $model->name, '{{sitename}}' => Cii::getConfig('name', Yii::app()->name)));
70
		$this->render('index', array(
71
			'model' => $model,
72
			'md' => new CMarkdownParser
73
		));
74
	}
75
76
	/**
77
	 * Provides functionality for a user to edit their profile
78
	 */
79
	public function actionEdit()
0 ignored issues
show
Coding Style introduced by
actionEdit uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
80
	{
81
		$model = new ProfileForm;
82
		$model->load(Yii::app()->user->id);
83
84
		if (Cii::get($_POST, 'ProfileForm', NULL) !== NULL)
85
		{
86
			$model->attributes = $_POST['ProfileForm'];
87
			$model->password_repeat = $_POST['ProfileForm']['password_repeat'];
88
89
			if ($model->save())
90
			{
91
				Yii::app()->user->setFlash('success', Yii::t('ciims.controllers.Profile', 'Your profile has been updated!'));
92
				$this->redirect($this->createUrl('profile/index', array(
93
					'id' => $model->id,
94
					'username' => $model->username
95
				)));
96
			}
97
			else
98
				Yii::app()->user->setFlash('error', Yii::t('ciims.controllers.Profile', 'There were errors saving your profile. Please correct them before trying to save again.'));
99
		}
100
101
		$this->render('edit', array(
102
			'model' => $model
103
		));
104
	}
105
106
	/**
107
	 * Send a new verification email to the user
108
	 */
109
	public function actionResend()
110
	{
111
		$model = new ProfileForm;
112
		$model->load(Yii::app()->user->id);
113
114
		// If we don't have one on file, then someone the user got to a page they shouldn't have gotten to
115
		// Seamlessly redirect them back
116
		if ($model->getNewEmail() == NULL)
117
			$this->redirect(Yii::app()->user->returnUrl);
118
119
		if ($model->sendVerificationEmail())
120
			Yii::app()->user->setFlash('success', Yii::t('ciims.controllers.Profile', 'A new verification email has been resent to {{user}}. Please check your email address.', array(
121
				'{{user}}' => $model->getNewEmail()
122
			)));
123
		else
124
			Yii::app()->user->setFlash('error', Yii::t('ciims.controllers.Profile', 'There was an error resending the verification email. Please try again later.'));
125
126
		$this->redirect($this->createUrl('profile/edit'));
127
	}
128
}
129