SessionAuth::current()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Ntb\RestAPI;
4
5
/**
6
 * Authentication mechanism which uses the internal Silverstripe authentication (session based).
7
 * @author Christian Blank <[email protected]>
8
 */
9
class SessionAuth extends \SS_Object implements IAuth {
10
11
    public static function authenticate($email, $password) {
12
        // auth
13
        $authenticator = \Injector::inst()->get('ApiMemberAuthenticator');
14 View Code Duplication
        if($user = $authenticator->authenticate(['Password' => $password, 'Email' => $email])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
	        return self::createSession($user);
16
        }
17
    }
18
19
	/**
20
	 * @param \Member $user
21
	 * @return ApiSession
22
	 */
23 View Code Duplication
	public static function createSession($user) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
		$user->logIn();
25
		/** @var \Member $user */
26
		$user = \DataObject::get(\Config::inst()->get('BaseRestController', 'Owner'))->byID($user->ID);
27
		// create session
28
		$session = ApiSession::create();
29
		$session->User = $user;
30
		$session->Token = AuthFactory::generate_token($user);
31
		return $session;
32
	}
33
34
    /**
35
     * @param \SS_HTTPRequest $request
36
     * @return bool
37
     * @throws RestUserException
38
     */
39 View Code Duplication
	public static function delete($request) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
        $owner = self::current($request);
41
        if(!$owner) {
42
            throw new RestUserException("No session found", 404, 404);
43
        }
44
        $owner->logOut();
45
        return true;
46
    }
47
48
    /**
49
     * @param \SS_HTTPRequest $request
50
     * @return \Member
0 ignored issues
show
Documentation introduced by
Should the return type not be \Member|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
51
     */
52
    public static function current($request) {
53
        $id = \Member::currentUserID();
54
        return $id ? \DataObject::get(\Config::inst()->get('BaseRestController', 'Owner'))->byID($id) : null;
55
    }
56
}
57