1 | <?php defined('SYSPATH') OR die('No direct access allowed.'); |
||
10 | abstract class Kohana_Auth_Service { |
||
11 | |||
12 | protected $_service_field; |
||
13 | |||
14 | protected $_type; |
||
15 | |||
16 | protected $_api; |
||
17 | |||
18 | protected $_enabled; |
||
19 | |||
20 | protected $_config = array(); |
||
21 | |||
22 | protected $_login_role; |
||
23 | |||
24 | protected $_user_model = 'user'; |
||
25 | |||
26 | protected $_role_model = 'role'; |
||
27 | |||
28 | 3 | public function api($api = NULL) |
|
29 | { |
||
30 | 3 | if ( ! $this->_api) |
|
31 | { |
||
32 | 3 | $this->_api = $api ?: $this->initialize(); |
|
33 | } |
||
34 | 3 | return $this->_api; |
|
35 | } |
||
36 | |||
37 | 37 | public function __construct($config = NULL) |
|
38 | { |
||
39 | 37 | $this->_config = (array) $config; |
|
40 | 37 | $this->_enabled = Arr::get($this->_config, 'enabled', FALSE); |
|
41 | 37 | } |
|
42 | |||
43 | 1 | public function type() |
|
44 | { |
||
45 | 1 | return $this->_type; |
|
46 | } |
||
47 | |||
48 | 12 | public function auto_login_enabled() |
|
49 | { |
||
50 | 12 | return Arr::get($this->_config, 'auto_login', FALSE); |
|
51 | } |
||
52 | |||
53 | 11 | public function enabled($enabled = NULL) |
|
54 | { |
||
55 | 11 | if ($enabled !== NULL) |
|
56 | { |
||
57 | $this->_enabled = $enabled; |
||
58 | return $this; |
||
59 | } |
||
60 | 11 | return $this->_enabled; |
|
61 | } |
||
62 | |||
63 | 1 | public function build_user($data, $create = TRUE) |
|
64 | { |
||
65 | 1 | if ($this->logged_in() AND ! empty($data)) |
|
66 | { |
||
67 | 1 | $user = Jam::build($this->_user_model); |
|
68 | |||
69 | 1 | if ($user->load_service_values($this, $data, $create) === FALSE) |
|
70 | return FALSE; |
||
71 | |||
72 | 1 | $user->roles->add(Jam::find($this->_role_model, 'login')); |
|
73 | |||
74 | 1 | $user->set($this->_service_field, $this->service_uid()); |
|
75 | |||
76 | 1 | return $user; |
|
77 | } |
||
78 | } |
||
79 | |||
80 | 10 | public function get_user() |
|
81 | { |
||
82 | 10 | if ($this->enabled() AND $this->logged_in()) |
|
83 | { |
||
84 | 1 | $user = Jam::find_or_build($this->_user_model, array($this->_service_field => $this->service_uid())); |
|
85 | 1 | $user->_is_new = TRUE; |
|
86 | 1 | $data = $this->service_user_info(); |
|
87 | |||
88 | 1 | if ( ! $user->loaded()) |
|
89 | { |
||
90 | 1 | if (isset($data['email'])) |
|
91 | { |
||
92 | 1 | $user = Jam::find_or_build($this->_user_model, array('email' => $data['email'])); |
|
93 | |||
94 | 1 | if ($user->loaded()) |
|
95 | { |
||
96 | 1 | $user->_is_new = FALSE; |
|
97 | |||
98 | 1 | if (Arr::get($this->_config, 'update_user_on_link')) |
|
99 | { |
||
100 | $user->load_service_values($this, $data, FALSE); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | 1 | if ( ! $user->loaded() AND Arr::get($this->_config, 'create_user')) |
|
106 | { |
||
107 | $user = $this->build_user($data, TRUE); |
||
108 | $user->_is_new = TRUE; |
||
109 | } |
||
110 | |||
111 | 1 | if ( ! $user) |
|
112 | { |
||
113 | throw new Auth_Exception_Service('Service :service user with service uid :id does not exist and failed to create', array( |
||
114 | ':service' => $this->type(), |
||
115 | ':id' => $this->service_uid() |
||
116 | )); |
||
117 | } |
||
118 | |||
119 | 1 | $user->set($this->_service_field, $this->service_uid()); |
|
120 | 1 | $user->save(); |
|
121 | } |
||
122 | 1 | elseif (Arr::get($this->_config, 'update_user')) |
|
123 | { |
||
124 | $user->_is_new = FALSE; |
||
125 | $user->load_service_values($this, $data, FALSE); |
||
126 | $user->save(); |
||
127 | } |
||
128 | else |
||
129 | { |
||
130 | 1 | $user->_is_new = FALSE; |
|
131 | } |
||
132 | 1 | return $user; |
|
133 | } |
||
134 | 9 | return FALSE; |
|
135 | } |
||
136 | |||
137 | public function logout() |
||
138 | { |
||
139 | if ( ! $this->enabled()) |
||
140 | return FALSE; |
||
141 | |||
142 | return $this->logout_service(Request::initial(), URL::site(Request::current()->uri(), TRUE)); |
||
|
|||
143 | } |
||
144 | |||
145 | public function login() |
||
146 | { |
||
147 | if ( ! $this->enabled()) |
||
148 | return FALSE; |
||
149 | |||
150 | if (($user = $this->get_user())) |
||
151 | { |
||
152 | return $user; |
||
153 | } |
||
154 | else |
||
155 | { |
||
156 | $login_url = $this->login_url(URL::site(Arr::get($this->_config, 'back_url', Request::current()->uri()), TRUE)); |
||
157 | |||
158 | HTTP::redirect($login_url); |
||
159 | return FALSE; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | public function complete_login() |
||
164 | { |
||
165 | if ( ! $this->enabled()) |
||
166 | return FALSE; |
||
167 | |||
168 | if ( ! $this->logged_in()) { |
||
169 | $this->service_login_complete(); |
||
170 | } |
||
171 | |||
172 | if (($user = $this->get_user())) |
||
173 | { |
||
174 | return $user; |
||
175 | } |
||
176 | |||
177 | return FALSE; |
||
178 | } |
||
179 | |||
180 | abstract public function initialize(); |
||
181 | |||
182 | abstract public function logged_in(); |
||
183 | |||
184 | abstract public function service_login_complete(); |
||
185 | |||
186 | abstract public function login_url($back_url); |
||
187 | |||
188 | abstract public function logout_service($request, $back_url); |
||
189 | |||
190 | abstract public function service_user_info(); |
||
191 | |||
192 | abstract public function service_uid(); |
||
193 | |||
194 | } |
||
195 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.