UserController::deauthenticate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 18
c 1
b 1
f 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php 
2
3
namespace App\Http\Controllers\Api\V1;
4
5
use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;
6
use Chrisbjr\ApiGuard\Models\ApiKey;
7
use App\LaravelRestCms\User\User as User;
8
use Input;
9
use Validator;
10
11
class UserController extends ApiGuardController
12
{
13
	/**
14
	 * The name of the model to use for this package
15
	 * 
16
	 * @var string
17
	 */
18
	protected $modelName = \App\LaravelRestCms\User\User::class;
19
    
20
	/**
21
	 * The name of the transformer to use for this package
22
	 * 
23
	 * @var string
24
	 */
25
	protected $transformerName = \App\LaravelRestCms\User\UserTransformer::class;
26
    
27
	/**
28
	 * The key to use as a key for this collection in the output
29
	 * 
30
	 * @var string
31
	 */
32
	protected $collectionName = 'users';
33
34
	/**
35
	 * The methods that don't require api authentication
36
	 * 
37
	 * @var array
38
	 */
39
	protected $apiMethods = [
40
		'authenticate' => [
41
			'keyAuthentication' => false
42
		],
43
		'deauthenticate' => [
44
			'keyAuthentication' => false
45
		]
46
	];
47
48
	/**
49
	 * Authenticate the login
50
	 * 
51
	 * @return \Illuminate\Http\JsonResponse|\Illuminate\Contracts\Routing\ResponseFactory
52
	 */
53
	public function authenticate() 
54
	{
55
		$credentials = [
56
			'username' => Input::get('username'),
57
			'password' => Input::get('password'),
58
		];
59
        
60
		$validator = Validator::make([
61
				'username' => $credentials['username'],
62
				'password' => $credentials['password']
63
			],
64
			[
65
				'username' => 'required|max:255',
66
				'password' => 'required|max:255'
67
			]
68
		);
69
        
70
		if ($validator->fails()) {
71
			return $this->response->errorWrongArgsValidator($validator);
0 ignored issues
show
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72
		}
73
74
		try {
75
			$user                 = with(new User)->authenticate($credentials['username'], $credentials['password'])->first();
76
			$credentials['email'] = $user->email;
77
		} catch (\ErrorException $e) {
78
			return $this->response->errorUnauthorized("Your username or password is incorrect");
79
		}
80
81
		// User validated, now assign an api key for this session
82
		$apiKey = ApiKey::where('user_id', '=', $user->id)->first();
83
        
84
		if (!isset($apiKey)) {
85
			$apiKey                = new ApiKey;
86
			$apiKey->user_id       = $user->id;
87
			$apiKey->key           = $apiKey->generateKey();
88
			$apiKey->level         = 5;
89
			$apiKey->ignore_limits = 0;
90
		} else {
91
			$apiKey->generateKey();
92
		}
93
94
		if (!$apiKey->save()) {
95
			return $this->response->errorInternalError("Failed to create an API key. Please try again.");
96
		}
97
98
		// return api key
99
		return $this->response->withItem($user, new \App\LaravelRestCms\User\UserTransformer);
100
	}
101
102
	/**
103
	 * Retrieve the user model
104
	 * 
105
	 * @return Model
106
	 */
107
	public function getUserDetails() 
108
	{
109
		$user = $this->apiKey->user;
110
111
		return isset($user) ? $user : $this->response->errorNotFound();
112
	}
113
114
	/**
115
	 * Log the user out
116
	 * 
117
	 * @param  string $apiKey
118
	 * @return \Illuminate\Http\JsonResponse
119
	 */
120
	public function deauthenticate($apiKey) 
121
	{
122
		$this->apiKey = ApiKey::where('key', $apiKey)->first();
123
124
		if (empty($this->apiKey)) {
125
			return $this->response->errorUnauthorized("There is no such user to deauthenticate.");
126
		}
127
128
		$this->apiKey->delete();
129
130
		return $this->response->withArray([
131
			'ok' => [
132
				'code'      => 'SUCCESSFUL',
133
				'http_code' => 200,
134
				'message'   => 'User was successfuly deauthenticated'
135
			]
136
		]);
137
	}
138
}