Completed
Pull Request — master (#3)
by Bart
02:38
created

ApiAuthController::actionAuthenticate()   B

Complexity

Conditions 4
Paths 15

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 31
rs 8.5806
ccs 21
cts 21
cp 1
cc 4
eloc 20
nc 15
nop 0
crap 4
1
<?php
2
3
namespace Craft;
4
5
/**
6
 * Class ApiAuthController
7
 *
8
 * Api authentication using user keys
9
 *
10
 * @author    Nerds & Company
11
 * @copyright Copyright (c) 2015, Nerds & Company
12
 * @license   MIT
13
 *
14
 * @link      http://www.nerds.company
15
 */
16
class ApiAuthController extends BaseController
17
{
18
    /** @var bool */
19
    protected $allowAnonymous = array('authenticate', 'resetPassword');
20
21
    /**
22
     * Set cors headers and check for options request
23
     */
24
    public function init()
25
    {
26
        craft()->apiAuth->setCorsHeaders();
27
        if (craft()->apiAuth->isOptionsRequest()) {
28
            craft()->end();
29
        }
30
    }
31
32
    /**
33
     * Authenticate action.
34
     */
35 4
    public function actionAuthenticate()
36
    {
37
38
        try {
39 4
            $this->requirePostRequest();
40
41 3
            $username = craft()->request->getRequiredPost('username');
42 3
            $password = craft()->request->getRequiredPost('password');
43
44 3
            if (craft()->userSession->login($username, $password)) {
45 2
                $key = craft()->apiAuth->generateKey();
46 2
                $user = craft()->userSession->getUser();
47
48 2
                if (craft()->apiAuth->saveKey($user, $key)) {
49 1
                    $this->returnJson(array(
50 1
                        'key' => $key,
51 1
                    ));
52 1
                } else {
53 1
                    HeaderHelper::setHeader('HTTP/ 500 Internal server error');
54 1
                    $this->returnErrorJson(Craft::t('Something went wrong'));
55
                }
56 2
            } else {
57 1
                HeaderHelper::setHeader('HTTP/ 401 Bad Credentials');
58 1
                $this->returnErrorJson(Craft::t('Invalid username or password'));
59
            }
60
61 4
        } catch (HttpException $e) {
0 ignored issues
show
Bug introduced by
The class Craft\HttpException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
62 1
            HeaderHelper::setHeader('HTTP/ ' . $e->statusCode);
63 1
            $this->returnErrorJson($e->getMessage());
64
        }
65 4
    }
66
67
    /**
68
     * Forgot password action
69
     */
70 3
    public function actionResetPassword()
71
    {
72
73
        try {
74 3
            $this->requirePostRequest();
75
76 2
            $username = craft()->request->getRequiredPost('username');
77 2
            $user = craft()->users->getUserByUsernameOrEmail($username);
78
79 2
            if ($user) {
80 1
                craft()->users->sendPasswordResetEmail($user);
81 1
            }
82
83 2
            $this->returnJson(array('message' => Craft::t('Email has been sent if address exists')));
84
85 3
        } catch (HttpException $e) {
0 ignored issues
show
Bug introduced by
The class Craft\HttpException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
86 1
            HeaderHelper::setHeader('HTTP/ ' . $e->statusCode);
87 1
            $this->returnErrorJson($e->getMessage());
88
        }
89 3
    }
90
}
91