ApiAuthController::actionResetPassword()   A
last analyzed

Complexity

Conditions 3
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 11
c 2
b 1
f 1
nc 8
nop 0
dl 0
loc 19
rs 9.4285
ccs 12
cts 12
cp 1
crap 3
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
        try {
38 4
            $this->requirePostRequest();
39
40 3
            $username = craft()->request->getRequiredPost('username');
41 3
            $password = craft()->request->getRequiredPost('password');
42
43 3
            if (craft()->userSession->login($username, $password)) {
44 2
                $key = craft()->apiAuth->generateKey();
45 2
                $user = craft()->userSession->getUser();
46
47 2
                if (craft()->apiAuth->saveKey($user, $key)) {
48 1
                    $this->returnJson(array(
49 1
                        'key' => $key,
50 1
                        'user' => $this->extractUserData($user),
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 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...
61 1
            HeaderHelper::setHeader('HTTP/ ' . $e->statusCode);
62 1
            $this->returnErrorJson($e->getMessage());
63
        }
64 4
    }
65
66
    /**
67
     * Forgot password action
68
     */
69 3
    public function actionResetPassword()
70
    {
71
        try {
72 3
            $this->requirePostRequest();
73
74 2
            $username = craft()->request->getRequiredPost('username');
75 2
            $user = craft()->users->getUserByUsernameOrEmail($username);
76
77 2
            if ($user) {
78 1
                craft()->users->sendPasswordResetEmail($user);
79 1
            }
80
81 2
            $this->returnJson(array('message' => Craft::t('Email has been sent if address exists')));
82
83 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...
84 1
            HeaderHelper::setHeader('HTTP/ ' . $e->statusCode);
85 1
            $this->returnErrorJson($e->getMessage());
86
        }
87 3
    }
88
89
    /**
90
     * Exposes interesting user fields to the API.
91
     *
92
     * @param UserModel $user
93
     *
94
     * @return array
95
     */
96 1
    private function extractUserData(UserModel $user)
97
    {
98
        return array(
99 1
            'username'  => $user->username,
100 1
            'photo'     => $user->photo,
101 1
            'firstName' => $user->firstName,
102 1
            'lastName'  => $user->lastName,
103 1
            'email'     => $user->email,
104 1
        );
105
    }
106
}
107