PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/api/versions/v1/controllers/AuthController.php

https://gitlab.com/Sang240892/real-estate-system
PHP | 223 lines | 169 code | 35 blank | 19 comment | 19 complexity | 3095ba0572142a18597c517978600fa5 MD5 | raw file
  1. <?php
  2. /**
  3. * User: sangnguyen
  4. * Date: 9/29/15
  5. * Time: 14:24
  6. * File name: UserController.php
  7. * Project name: Fit Road
  8. */
  9. namespace api\versions\v1\controllers;
  10. use api\commons\forms\SentTokenRestPasswordForm;
  11. use api\commons\forms\SignUpForm;
  12. use api\commons\helpers\ApiHelpers;
  13. use api\components\RestController;
  14. use backend\commons\helpers\UtilHelper;
  15. use common\models\entities\NotificationCategoryRule;
  16. use common\models\entities\RealEstateCategory;
  17. use common\models\entities\RealEstateUser;
  18. use common\models\UserIdentity;
  19. use Yii;
  20. Class AuthController extends RestController{
  21. /**
  22. * @var: User Login
  23. */
  24. public function actionVerify(){
  25. // $this->requireDeviceToken();
  26. $form = new \api\commons\forms\SignInForm();
  27. $form->email = isset ($this->request->email) ? $this->request->email : null;
  28. $form->password = isset ($this->request->password) ? $this->request->password : null;
  29. $deviceToken = isset($this->request->authentication->instanceIDToken)?(string)$this->request->authentication->instanceIDToken:null;
  30. $deviceType = isset($this->request->authentication->deviceType)?(string)$this->request->authentication->deviceType:null;
  31. $osType = isset($this->request->authentication->osType)?(string)$this->request->authentication->osType:null;
  32. if($form->validate()){
  33. $form->login();
  34. $userIndentity = Yii::$app->user->getIdentity();
  35. $this->user = $userIndentity;
  36. $tokenParams = [
  37. 'user_id' => $userIndentity->id,
  38. 'auth_token' => Yii::$app->security->generateRandomString(),
  39. 'device_token' => $deviceToken,
  40. 'device_type' => $deviceType,
  41. 'os_type' => $osType,
  42. 'user_host' => Yii::$app->request->userHost,
  43. 'user_ip' =>Yii::$app->request->userIP,
  44. 'last_accessed' => UtilHelper::getUnixUTCMinuteAfter(UserIdentity::EXPIRED_TIME) //expired time
  45. ];
  46. $userSelf = RealEstateUser::findOne(['id'=>$userIndentity->id]);
  47. $token = $userSelf->register($this->userRepository)->verifyAuthKey($tokenParams);
  48. if(!$token){
  49. $this->sendResponse(false,$this->builtErrorCode(100));
  50. }
  51. $this->outPutUser($userSelf,$token);
  52. }else{
  53. $this->getFormError($form);
  54. }
  55. }
  56. public function actionSignUp(){
  57. $businessMsg = null;
  58. $isUpdate = FALSE;
  59. $token = null;
  60. if (isset($this->request->authentication->authToken) && !empty($this->request->authentication->authToken)) {
  61. $this->requireAuthToken();
  62. $form = new SignUpForm(['scenario'=>'update']);
  63. $form->user = $this->user;
  64. $isUpdate = TRUE;
  65. }else{
  66. $form = new SignUpForm(['scenario'=>'register']);
  67. }
  68. // echo '<pre>';
  69. // print_r($form->getScenario());
  70. // echo '</pre>';
  71. // die();
  72. $form->email = isset ($this->request->email) ? $this->request->email : null;
  73. $form->password = isset ($this->request->password) ? (string)$this->request->password : null;
  74. $form->firstName = isset ($this->request->firstName) ? (string)$this->request->firstName : null;
  75. $form->lastName = isset ($this->request->lastName) ? (string)$this->request->lastName : null;
  76. $form->phone = isset ($this->request->phone) ? (string)$this->request->phone : null;
  77. $form->isNotify = isset ($this->request->isNotify) ? (int)$this->request->isNotify : 0;
  78. $form->notificationCityRule = isset ($this->request->notifyCityRule) ? (string)$this->request->notifyCityRule : null;
  79. $form->notificationMinPriceRule = isset ($this->request->notifyMinPriceRule) ? (int)$this->request->notifyMinPriceRule : null;
  80. $form->notificationMaxPriceRule = isset ($this->request->notifyMaxPriceRule) ? (int)$this->request->notifyMaxPriceRule : null;
  81. $notificationCategories = isset ($this->request->notifyCategories) ? $this->request->notifyCategories : null;
  82. $deviceToken = isset($this->request->authentication->instanceIDToken)?(string)$this->request->authentication->instanceIDToken:null;
  83. $deviceType = isset($this->request->authentication->deviceType)?(string)$this->request->authentication->deviceType:null;
  84. $osType = isset($this->request->authentication->osType)?(string)$this->request->authentication->osType:null;
  85. if(empty($notificationCategories)){
  86. $this->sendResponse(['msgClient'=>Yii::t('app','Bạn phải chọn ít nhất một chuyện mục để nhận thông báo.')]);
  87. }
  88. if(empty($form->notificationCityRule)){
  89. $this->sendResponse(['msgClient'=>Yii::t('app','Bạn phải chọn khu vực tin bất động sản để nhận thông báo.')]);
  90. }
  91. if($form->validate() && $form->verify()){
  92. if($isUpdate === FALSE){
  93. $this->user = $form->user;
  94. $tokenParams = [
  95. 'user_id' => $this->user->id,
  96. 'auth_token' => Yii::$app->security->generateRandomString(),
  97. 'device_token' => $deviceToken,
  98. 'device_type' => $deviceType,
  99. 'os_type' => $osType,
  100. 'user_host' => Yii::$app->request->userHost,
  101. 'user_ip' =>Yii::$app->request->userIP,
  102. 'last_accessed' => UtilHelper::getUnixUTCMinuteAfter(UserIdentity::EXPIRED_TIME) //expired time
  103. ];
  104. $token = $this->user->register($this->userRepository)->verifyAuthKey($tokenParams);
  105. }
  106. if(!$token && $isUpdate === FALSE){
  107. $this->sendResponse(false,$this->builtErrorCode(100));
  108. }
  109. $this->user->notify_categories = json_encode($notificationCategories);
  110. $this->user->save();
  111. // add category notification rules
  112. if(!empty($notificationCategories)){
  113. $rules = $this->user->getNotificationCategoryRules()->all();
  114. if(!empty($rules)){
  115. foreach($rules as $r){
  116. $r->delete();
  117. }
  118. }
  119. foreach($notificationCategories as $cate){
  120. $category = RealEstateCategory::findOne(['id'=>$cate]);
  121. if($category){
  122. $finders = $category->getRealEstateCategoryRelations()->all();
  123. if(!empty($finders)){
  124. foreach($finders as $finder){
  125. try{
  126. $notification = new NotificationCategoryRule();
  127. $notification->user_id = $this->user->id;
  128. $notification->category_id = $finder->category_children_id;
  129. $notification->save();
  130. }catch (\yii\db\Exception $e){
  131. Yii::error('Error \'s name: '.$e->getName(), 'Users');
  132. Yii::error('Error \'s message: '.$e->getMessage(), 'Users');
  133. Yii::error('Error get data self contact', 'Users');
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. $this->outPutUser($this->user,$token);
  141. }else{
  142. $this->getFormError($form);
  143. }
  144. }
  145. public function actionSignOut(){
  146. $authToken = $this->requireAuthToken();
  147. $authToken->user->is_online = 0;
  148. if( $authToken->user->save(false)){
  149. Yii::$app->user->logout();
  150. try{
  151. $authToken->delete();
  152. $this->sendResponse();
  153. }
  154. catch (\yii\db\Exception $e){
  155. Yii::error('Error \'s name: '.$e->getName(), 'Users');
  156. Yii::error('Error \'s message: '.$e->getMessage(), 'Users');
  157. Yii::error('Error Delete authetication token of user '.Yii::$app->user->id, 'Users');
  158. }
  159. }
  160. $this->sendResponse(false,$this->builtErrorCode(400));
  161. }
  162. /**
  163. * @var : Password reset request
  164. */
  165. public function actionPasswordResetRequest(){
  166. $form = new SentTokenRestPasswordForm();
  167. $form->isBackend = FALSE;
  168. $form->email = isset ($this->request->email) ? $this->request->email : null;
  169. if($form->validate()){
  170. $send = $form->sendEmail();
  171. if($send == true)
  172. $this->sendResponse(['msgClient'=>Yii::t('app','Please check mail.')]);
  173. else
  174. $this->sendResponse(false,$this->builtErrorCode(94));
  175. }else{
  176. $this->getFormError($form);
  177. }
  178. }
  179. public function actionInstanceidtoken(){
  180. $authToken = $this->requireAuthToken();
  181. $deviceToken = isset($this->request->authentication->instanceIDToken)?(string)$this->request->authentication->instanceIDToken:null;
  182. $osType = isset($this->request->authentication->osType)?(int)$this->request->authentication->osType:null;
  183. $authToken->device_token = $deviceToken;
  184. $authToken->os_type =$osType;
  185. $authToken->save(false);
  186. $this->sendResponse();
  187. }
  188. }