HEAD ======= >>>>>>> release-0.2.0
1 <?php
2 /**
3 * Copyright 2012 Native5. All Rights Reserved
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * You may not use this file except in compliance with the License.
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 * PHP version 5.3+
14 *
15 * @category Users
16 * @package Native5\<package>
17 * @author Barada Sahu <barry@native5.com>
18 * @copyright 2012 Native5. All Rights Reserved
19 * @license See attached LICENSE for details
20 * @version GIT: $gitid$
21 * @link http://www.docs.native5.com
22 */
23
24 namespace Native5\Services\Users;
25
<<<<<<< HEAD
26 use Native5\Api\ApiClient;
=======
26 use Native5\Services\Common\ApiClient;
>>>>>>> release-0.2.0
27
28 /**
29 * %ClassName%
30 *
31 * @category Users
32 * @package Native5\Services\Users
33 * @author Barada Sahu <barry@native5.com>
34 * @copyright 2012 Native5. All Rights Reserved
35 * @license See attached NOTICE.md for details
36 * @version Release: 1.0
37 * @link http://www.docs.native5.com
38 * Created : 27-11-2012
39 * Last Modified : Fri Dec 21 09:11:53 2012
40 */
41 class DefaultUserManager extends ApiClient implements UserManager
42 {
43
44
45 /**
46 * deactivateUser
47 *
48 * @param mixed $user The user
49 *
50 * @access public
51 * @return void
52 */
53 public function deactivateUser($user)
54 {
55 $user->setState(UserState::INACTIVE);
56 // TODO : Update User
57 }
58
59
60 /**
61 * activateUser
62 *
63 * @param mixed $user The user
64 *
65 * @access public
66 * @return void
67 */
68 public function activateUser($user)
69 {
70 $user->setState(UserState::ACTIVE);
71 // TODO : Update user
72 }
73
74 /**
75 * getStatus
76 *
77 * @param mixed $user The user object
78 *
79 * @access public
80 * @return void
81 */
82 public function getStatus($user)
83 {
84 // TODO : Get user object
85 return $user->getState();
86 }
87
88
89 /**
90 * Authenticates the subject.
91 *
92 * @param mixed $token The token to authenticate
93 *
94 * @access public
95 * @return void
96 */
97 public function authenticate($token)
98 {
99 $path = 'users/authenticate';
100 $request = $this->_remoteServer->get($path);
101 $request->getQuery()->set('subject', $subject->serialize('json'));
102 $response = $request->send();
103 $result = $response->json();
104 if ($result->authenticated === true) {
105 $app->getSessionManager()->startSession(null, true);
106 $subject = SecurityUtils::getSubject();
107 // - Update Subject, based on incoming data.
108 // - Set subject authentication status = true
109 }
110 }
111
112
113 /**
114 * save
115 *
116 * @param mixed $user The user
117 *
118 * @access public
119 * @return void
120 */
121 public function save($user)
122 {
123 $logger = $GLOBALS['logger'];
124 $path = 'users/'.$user->getId();
125 $request = $this->_remoteServer->post($path)
126 ->setPostField('user', $user->serialize());
127 try {
128 $response = $request->send();
129 return $response->getBody('true');
130 } catch(\Guzzle\Http\Exception\BadResponseException $e) {
131 $logger->info($e->getResponse()->getBody('true'), array());
132 return false;
133 }
134 }
135
136
137 /**
138 * definePasswordPolicy
139 *
140 * @param mixed $policy Password policy
141 *
142 * @access public
143 * @return void
144 */
145 public function definePasswordPolicy($policy)
146 {
147 $policy = new PasswordPolicy($policy);
148 $policy->save();
149 return $policy;
150 }
151
152
153 /**
154 * applyPasswordPolicy
155 *
156 * @param mixed $policy Password policy to apply
157 * @param mixed $groups Groups on which policy must be enforced.
158 *
159 * @access public
160 * @return void
161 */
162 public function applyPasswordPolicy($policy, $groups)
163 {
164 // TODO : Store association of groups with policies
165 foreach ($groups as $group) {
166 $group->applyPolicy($policy);
167 }
168 }
169
170 /**
171 * changePassword
172 *
173 * @param mixed $email E-mail id of user
174 * @param mixed $token Token
175 * @param mixed $password New password
176 *
177 * @access public
178 * @return void
179 * @throws \Exception Password change failed
180 */
181 public function changePassword($email, $token, $password)
182 {
183 global $logger;
184 $path = 'users/password/change';
185 $request = $this->_remoteServer->post($path)
186 ->setPostField('email', $email)
187 ->setPostField('token', $token)
188 ->setPostField('password', $password);
189 try {
190 $response = $request->send();
191 } catch(\Guzzle\Http\Exception\BadResponseException $e) {
192 $logger->info($e->getResponse()->getBody('true'), array());
193 return false;
194 }
195 return true;
196
197 }//end changePassword()
198
199
200 /**
201 * verifyEmail
202 *
203 * @param mixed $email E-mail to verify.
204 *
205 * @access public
206 * @return void
207 */
208 public function verifyEmail($email)
209 {
210 $path = 'users/verify_email';
211 $request = $this->_remoteServer->get($path);
212 $request->getQuery()->set('email', $email);
213 $response = $request->send();
214 return $response->getBody('true');
215 }
216
217
218 /**
219 * Verifies token given the user id & the token.
220 *
221 * @param mixed $email User e-mail
222 * @param mixed $token API token
223 *
224 * @access public
225 * @return void
226 */
227 public function verifyToken($email, $token)
228 {
229 $path = 'users/verify_token';
230 $request = $this->_remoteServer->get($path);
231 $request->getQuery()->set('email', $email);
232 $request->getQuery()->set('token', $token);
233 try {
234 $response = $request->send();
235 } catch(\Guzzle\Http\Exception\BadResponseException $e) {
236 $logger->info($e->getResponse()->getBody('true'), array());
237 return false;
238 }
239 return true;
240
241 }//end verifyToken()
242 }
243 ?>
244