Advertisement
Guest User

sad

a guest
Sep 9th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 KB | None | 0 0
  1. <?php
  2.  
  3. namespace common\modules\User\controllers;
  4. use common\modules\Chat\Chat;
  5. use common\modules\Chat\models\ChatGroup;
  6. use common\modules\Chat\models\Chatteh;
  7. use common\modules\Chat\models\UsersGroup;
  8. use common\modules\Project\models\Project;
  9. use common\modules\User\models\CliUsersActivity;
  10. use common\modules\User\models\UserReset;
  11. use frontend\models\SignupForm;
  12. use common\modules\User\models\Profile;
  13. use Yii;
  14. use common\modules\User\models\User;
  15. use common\modules\User\models\UserSearch;
  16. use yii\helpers\Url;
  17. use yii\web\Controller;
  18. use yii\web\NotFoundHttpException;
  19. use yii\filters\VerbFilter;
  20.  
  21. /**
  22. * UserController implements the CRUD actions for User model.
  23. */
  24. class UserController extends Controller
  25. {
  26. public function behaviors()
  27. {
  28. return [
  29. 'verbs' => [
  30. 'class' => VerbFilter::className(),
  31. 'actions' => [
  32. 'delete' => ['POST'],
  33. ],
  34. ],
  35. ];
  36. }
  37. public function actionIndex()
  38. {
  39. $searchModel = new UserSearch();
  40. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  41.  
  42. return $this->render('index', [
  43. 'searchModel' => $searchModel,
  44. 'dataProvider' => $dataProvider,
  45. ]);
  46. }
  47. public function actionView($id)
  48. {
  49. return $this->render('view', [
  50. 'model' => $this->findModel($id),
  51. ]);
  52. }
  53. public function actionCreate()
  54. {
  55. $model = new User();
  56.  
  57. if ($model->load(Yii::$app->request->post())) {
  58. $model->password_hash= Yii::$app->security->generatePasswordHash($model->password_hash);
  59. if ($model->save()) {
  60. if($model->admin==1)
  61. {
  62. $auth = Yii::$app->authManager;
  63. $auth->assign($auth->getRole('Utilizator'), $model->id);
  64. }
  65. else{
  66. $auth = Yii::$app->authManager;
  67. $auth->assign($auth->getRole('Client'), $model->id);
  68.  
  69. }
  70. $profile = new Profile();
  71. $profile->user_id = $model->id;
  72. $profile->save();
  73. return $this->redirect(['view', 'id' => $model->id]);
  74. }
  75. } else {
  76. return $this->render('create', [
  77. 'model' => $model,
  78. ]);
  79. }
  80. }
  81. public function actionUpdate($id)
  82. {
  83. $model = $this->findModel($id);
  84. $admin=$model->admin;
  85. if ($model->load(Yii::$app->request->post())) {
  86. $model->password_hash = Yii::$app->security->generatePasswordHash($model->password_hash);
  87. if ($model->save()) {
  88. if($model->admin!=$admin)
  89. if($model->admin==1)
  90. {
  91. $auth = Yii::$app->authManager;
  92. $exist=$auth->getAssignment('Utilizator', $model->id);
  93. if(empty($exist))
  94. {
  95. $auth->assign($auth->getRole('Utilizator'), $model->id);
  96. }
  97.  
  98. }
  99. else{
  100. $auth = Yii::$app->authManager;
  101. $exist=$auth->getAssignment('Client', $model->id);
  102. if(empty($exist)) {
  103. $auth->assign($auth->getRole('Client'), $model->id);
  104. }
  105. }
  106. return $this->redirect(['view', 'id' => $model->id]);
  107. }
  108.  
  109.  
  110. } else {
  111. $model->password_hash="";
  112. return $this->render('update', [
  113. 'model' => $model,
  114. ]);
  115. }
  116. }
  117. public function actionDelete($id)
  118. {
  119. ChatGroup::deleteAll(["author_id"=>$id]);
  120. UsersGroup::deleteAll(["userId"=>$id]);
  121. Chatteh::deleteAll(["userId"=>$id]);
  122. Chatteh::deleteAll(["to_user"=>$id]);
  123. CliUsersActivity::deleteAll(["user_id"=>$id]);
  124. Project::beforeDeleteUser($id);
  125. $this->findModel($id)->delete();
  126. return $this->redirect(['index']);
  127. }
  128. public function actionVerifyemail()
  129. {
  130. if (isset($_POST["code"])) {
  131. if (($_POST["code"] !=null)&&($_POST["code"] == Yii::$app->session->get("registcode"))) {
  132. return 1;
  133. }else
  134. return Yii::t("sy/site", "regist_error_message");
  135. }
  136. else {
  137.  
  138. if(!empty($_POST["email"])) {
  139.  
  140. $code = Yii::$app->security->generateRandomString(8);
  141. Yii::$app->session->set("registcode", $code);
  142. Yii::$app->mailer->compose()
  143. ->setFrom("stajor.it@gmail.com")
  144. ->setTo($_POST["email"])
  145. ->setSubject('Regist in SYCO')
  146. ->setTextBody('Date pentru autentificare in portalul Synapsys Community, REGIST CODE:' . $code)
  147. ->send();
  148.  
  149. return Yii::t("sy/user","Verifica cutia postala:") . $_POST["email"];
  150. }
  151. else return Yii::t("sy/user","Your must type your email adress!!");
  152. }
  153. }
  154. protected function findModel($id)
  155. {
  156. if (($model = User::findOne($id)) !== null) {
  157. return $model;
  158. } else {
  159. throw new NotFoundHttpException('The requested page does not exist.');
  160. }
  161. }
  162. public function actionResetpassword()
  163. {
  164.  
  165.  
  166. if(isset($_GET["token"])&&isset($_GET["user"]))
  167. {
  168. $control=UserReset::findOne(["user_id"=>$_GET["user"],"token"=>$_GET["token"]]);
  169.  
  170. if(isset($control))
  171. {
  172. $now = date("Y-m-d H:i:s");
  173.  
  174. if((strtotime($now)-strtotime($control->create_datetime))<300) {
  175. if (isset($_POST["User"])) {
  176. $user = User::findOne(["id" => Yii::$app->user->id]);
  177. $user->password_hash = $_POST["User"]["password_hash"];
  178. $user->save();
  179. return $this->goHome();
  180. } else {
  181. $user = User::findOne(["id" => $_GET["user"]]);
  182. Yii::$app->user->login($user, null);
  183. return $this->render("resetPassword");
  184. }
  185. }
  186. else return $this->render("ErrorPage");
  187. }
  188. }
  189. else{
  190. if(isset($_POST["email"])) {
  191.  
  192. $profile=Profile::findOne(["email"=>$_POST["email"]]);
  193. if(!empty($profile)) {
  194. $user = User::findOne($profile->user_id);
  195. $password = Yii::$app->getSecurity()->generateRandomString(64);
  196. $reset=new UserReset();
  197. $reset->user_id=$user->id;
  198. $reset->token=$password;
  199. $reset->save();
  200. Yii::$app->mailer->compose()
  201. ->setFrom("stajor.it@gmail.com")
  202. ->setTo($_POST["email"])
  203. ->setSubject('Datele de autentificare prin intermediu SYCO')
  204. ->setTextBody('Date pentru autentificare in portalul Synapsys Community')
  205. ->setHtmlBody(Yii::t("sy/user",'Acesati acest link pentru resetarea parole valabil 5 min:').'<a href="'.Url::base().Url::current(["user"=>$user->id,"token"=>$password]).'">Link pentru resetare</a>')
  206. ->send();
  207. return "<h3>".Yii::t("sy/user","Your reset link has been sending on your email,this link is valid 5 min").":".$_POST["email"]."</h3>";
  208. }
  209. else
  210. return "<h3>".Yii::t("sy/user","Email is dont'registed in SYCO")."</h3>";
  211. }
  212. }
  213. }
  214. public function actionDeletechaths()
  215. {
  216. Chatteh::deleteAll(["to_user"=>Yii::$app->user->id]);
  217. Chatteh::deleteAll(["userId"=>Yii::$app->user->id]);
  218. return $this->renderAjax('delhistory');
  219. }
  220. public function actionNewpassword()
  221. {
  222. if(isset($_POST["useroldpassword"])&&isset($_POST["password_hash"]))
  223. {
  224. $user=User::findOne(Yii::$app->user->id);
  225. $oldpass=$_POST["useroldpassword"];
  226. if($user->validatePassword($oldpass))
  227. {
  228. $user->setPassword($_POST["password_hash"]);
  229. $user->save();
  230. return $this->renderAjax('success_mess');
  231. }
  232. else return "false";
  233. }
  234. /*else
  235. if(isset($_POST["User"]))
  236. {
  237. $user=User::findOne(Yii::$app->user->id);
  238. $oldpass=$_POST["User"]["user-old-password"];
  239. if($user->validatePassword($oldpass))
  240. {
  241. $user->setPassword($_POST["User"]["password_hash"]);
  242. $user->save();
  243. return $this->goHome();
  244. }
  245. else return false;
  246.  
  247.  
  248. }*/
  249. else
  250. {
  251. return $this->renderAjax('newPassword',["model"=>new User()]);
  252. }
  253.  
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement