OpauthLoginForm::defineStrategyHandlers()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
/**
4
 * OpauthLoginForm
5
 * The form presented to users for signing in with an Opauth strategy.
6
 * Not a form, rather a gateway that works by taking enabled strategies and
7
 * displaying a button to start the OAuth process with that strategy provider.
8
 * @author Will Morgan <@willmorgan>
9
 * @author Dan Hensby <@dhensby>
10
 * @copyright Copyright (c) 2013, Better Brief LLP
11
 */
12
class OpauthLoginForm extends LoginForm {
0 ignored issues
show
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...
13
14
	private
15
		/*
16
		 * @var boolean
17
		 */
18
		$_strategiesDefined = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_strategiesDefined.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
19
20
	protected
21
		/**
22
		 * @var array config
23
		 */
24
		$authenticator_class = 'OpauthAuthenticator';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $authenticator_class.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
25
26
	private static
27
		$allowed_actions = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $allowed_actions.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
			'httpSubmission',
29
		);
30
31
	public function __construct($controller, $name) {
32
		parent::__construct($controller, $name, $this->getFields(), $this->getActions());
33
		$this->configureBackURL();
34
	}
35
36
	/**
37
	 * Handle any backURL. Uses sessions as state gets lost through OAuth flow.
38
	 * Use the same session key as MemberLoginForm for x-compat
39
	 */
40
	public function configureBackURL() {
41
		if($backURL = $this->controller->request->requestVar('BackURL')) {
42
			Session::set('BackURL', $backURL);
43
		}
44
	}
45
46
	/**
47
	 * Override httpSubmission so we definitely have strategy handlers.
48
	 * This is because Form::httpSubmission is directly called.
49
	 */
50
	public function httpSubmission($request) {
51
		$this->defineStrategyHandlers();
52
		return parent::httpSubmission($request);
53
	}
54
55
	/**
56
	 * Channel several unknown strategies in to one handler
57
	 */
58
	protected function defineStrategyHandlers() {
59
		if(!$this->_strategiesDefined) {
60
			foreach($this->getStrategies() as $strategyClass) {
61
				$strategyMethod = 'handleStrategy' . $strategyClass;
62
				$this->addWrapperMethod($strategyMethod, 'handleStrategy');
63
			}
64
			$this->_strategiesDefined = true;
65
		}
66
	}
67
68
	/**
69
	 * Ensure AuthenticationMethod is set to tell Security which form to process
70
	 * Very important for multi authenticator form setups.
71
	 * @return FieldList
72
	 */
73
	protected function getFields() {
74
		return new FieldList(
75
			new HiddenField('AuthenticationMethod', null, $this->authenticator_class)
76
		);
77
	}
78
79
	/**
80
	 * Provide an action button to be clicked per strategy
81
	 * @return FieldList
82
	 */
83
	protected function getActions() {
84
		$actions = new FieldList();
85
		foreach($this->getStrategies() as $strategyClass) {
86
			$strategyMethod = 'handleStrategy' . $strategyClass;
87
			$fa = new FormAction($strategyMethod, $strategyClass);
88
			$fa->setUseButtonTag(true);
89
			$actions->push($fa);
90
		}
91
		return $actions;
92
	}
93
94
	/**
95
	 * @return array All enabled strategies from config
96
	 */
97
	public function getStrategies() {
98
		return OpauthAuthenticator::get_enabled_strategies();
99
	}
100
101
	/**
102
	 * Global endpoint for handleStrategy - all strategy actions point here.
103
	 * @throws LogicException This should not be directly called.
104
	 * @throws InvalidArgumentException The strategy must be valid and existent
105
	 * @param string $funcName The bound function name from addWrapperMethod
106
	 * @param array $data Standard data param as part of form submission
107
	 * @param OpauthLoginForm $form
108
	 * @param SS_HTTPRequest $request
109
	 * @return ViewableData
110
	 */
111
	public function handleStrategy($funcName, $data, $form, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
		if(func_num_args() < 4) {
113
			throw new LogicException('Must be called with a strategy handler');
114
		}
115
		// Trim handleStrategy from the function name:
116
		$strategy = substr($funcName, strlen('handleStrategy')) . 'Strategy';
117
118
		// Check the strategy is good
119
		if(!class_exists($strategy) || $strategy instanceof OpauthStrategy) {
120
			throw new InvalidArgumentException('Opauth strategy ' . $strategy . ' was not found or is not a valid strategy');
121
		}
122
123
		return $this->controller->redirect(
124
			Controller::join_links(
125
				OpauthController::get_path(),
126
				OpauthAuthenticator::get_strategy_segment($strategy)
127
			)
128
		);
129
	}
130
131
	/**
132
	 * The authenticator name, used in templates
133
	 * @return string
134
	 */
135
	public function getAuthenticatorName() {
136
		return OpauthAuthenticator::get_name();
137
	}
138
139
}
140