AuthController::logout()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 0
crap 12
1
<?php
2
3
class AuthController extends PageController {
0 ignored issues
show
Coding Style introduced by
The property $register_valid_rules is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
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
	 * Page to show if user hasn't logged it but hit by auth filter
7
	 */
8
	public function loginPage() {
9
		$this->layout->title = 'Login';
10
		$this->layout->content = View::make('login');
11
	}
12
13
	/**
14
	 * Login with persona
15
	 *
16
	 * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
17
	 */
18
	public function login() {
19
		/* Force logout */
20
		if (! Auth::guest()) {
21
			Auth::logout();
22
		}
23
24
		if (! $assertion = Input::get("assertion")) {
25
			App::abort(401, 'i don\'t even');
26
		}
27
		// Check with persona
28
		try {
29
			$response = GuzzleHttp\post(
30
				'https://verifier.login.persona.org/verify',
31
				[
32
					'body' => [
33
						'assertion' => $assertion,
34
						'audience'  => url(),
35
					],
36
				]
37
			);
38
		} catch (GuzzleHttp\Exception\BadResponseException $e) {
39
			App::abort(401, 'You are not authorized.');
40
41
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by AuthController::login of type Illuminate\Http\JsonResp...e\Http\RedirectResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
42
		}
43
44
		$verification = $response->json();
45
		if ($verification['status'] != "okay") {
46
			App::abort(401, 'You are not authorized.');
47
48
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by AuthController::login of type Illuminate\Http\JsonResp...e\Http\RedirectResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
49
		}
50
		$email = $verification['email'];
51
52
		// Log in to laravel
53
		if (Auth::attempt(['email' => $email, 'password' => 'moz:persona'], true)) {
54
			if (Request::ajax()) {
55
				if(Session::has('url.intended')) {
56
					return Response::json(['redirect' => Session::get('url.intended')]);
57
				} else {
58
					return Response::json(['refresh' => true]);
59
				}
60
			} else {
61
				return Redirect::intended();
62
			}
63
		} else {
64
			// Not an user
65
			Session::put('register_email', $email);
66
			if (Request::ajax()) {
67
				return Response::json(['redirect' => route('auth.register')]);
68
			} else {
69
				return Redirect::route('auth.register.form');
70
			}
71
		}
72
73
	}
74
75
	/**
76
	 * Logout
77
	 *
78
	 * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
79
	 */
80
	public function logout() {
81
		if (Session::has("register_email")) {
82
			Session::forget("register_email");
83
		}
84
85
		Auth::logout();
86
87
		if (Request::ajax()) {
88
			return Response::json(["redirect" => url("/")]);
89
		} else {
90
			return Redirect::to("/");
91
		}
92
	}
93
94
	/**
95
	 * Registration validation rules
96
	 * @var array
97
	 */
98
	public $register_valid_rules = [
99
		'username'    => ['required', 'unique:users', 'min:2', 'max:16'],
100
		'displayname' => ['max:64'],
101
	];
102
103
	/**
104
	 * Registration form
105
	 *
106
	 * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
107
	 */
108
	public function registerForm() {
109
		if (! Session::has('register_email')) {
110
			return Redirect::home();
111
		}
112
113
		$this->layout->content = View::make("register", [
114
			'validation_rules' => $this->register_valid_rules,
115
			'email'            => Session::get('register_email'),
116
		]);
117
118
		return $this->layout;
119
	}
120
121
	/**
122
	 * Registration handler
123
	 *
124
	 * @return $this|\Illuminate\Http\RedirectResponse
125
	 */
126
	public function register() {
127
		if (! Session::has('register_email')) {
128
			return Redirect::home();
129
		}
130
131
		// Validate
132
		$validator = Validator::make(Input::all(), $this->register_valid_rules);
133
		if ($validator->fails()) {
134
			Notification::error("Something's wrong, check the fields bellow!");
135
136
			return Redirect::back()->withInput()->withErrors($validator);
137
		}
138
139
		// Create user
140
		$user = new User();
141
		$user->username = Input::get('username');
142
		$user->displayname = Input::get('displayname');
143
		$user->email = Session::get('register_email');
144
145
		// Save & Login
146
		if ($user->save()) {
147
			Auth::login($user, true);
148
			Session::forget('register_email');
149
150
			Notification::success("Welcome {$user->name}!");
151
152
			return Redirect::intended('/');
153
		} else {
154
			Notification::error("Save errors :'(");
155
156
			return Redirect::to('register')->withInput();
157
		}
158
	}
159
}