Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace common\modules\User\controllers;
- use common\modules\Chat\Chat;
- use common\modules\Chat\models\ChatGroup;
- use common\modules\Chat\models\Chatteh;
- use common\modules\Chat\models\UsersGroup;
- use common\modules\Project\models\Project;
- use common\modules\User\models\CliUsersActivity;
- use common\modules\User\models\UserReset;
- use frontend\models\SignupForm;
- use common\modules\User\models\Profile;
- use Yii;
- use common\modules\User\models\User;
- use common\modules\User\models\UserSearch;
- use yii\helpers\Url;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- /**
- * UserController implements the CRUD actions for User model.
- */
- class UserController extends Controller
- {
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['POST'],
- ],
- ],
- ];
- }
- public function actionIndex()
- {
- $searchModel = new UserSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- public function actionCreate()
- {
- $model = new User();
- if ($model->load(Yii::$app->request->post())) {
- $model->password_hash= Yii::$app->security->generatePasswordHash($model->password_hash);
- if ($model->save()) {
- if($model->admin==1)
- {
- $auth = Yii::$app->authManager;
- $auth->assign($auth->getRole('Utilizator'), $model->id);
- }
- else{
- $auth = Yii::$app->authManager;
- $auth->assign($auth->getRole('Client'), $model->id);
- }
- $profile = new Profile();
- $profile->user_id = $model->id;
- $profile->save();
- return $this->redirect(['view', 'id' => $model->id]);
- }
- } else {
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- }
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- $admin=$model->admin;
- if ($model->load(Yii::$app->request->post())) {
- $model->password_hash = Yii::$app->security->generatePasswordHash($model->password_hash);
- if ($model->save()) {
- if($model->admin!=$admin)
- if($model->admin==1)
- {
- $auth = Yii::$app->authManager;
- $exist=$auth->getAssignment('Utilizator', $model->id);
- if(empty($exist))
- {
- $auth->assign($auth->getRole('Utilizator'), $model->id);
- }
- }
- else{
- $auth = Yii::$app->authManager;
- $exist=$auth->getAssignment('Client', $model->id);
- if(empty($exist)) {
- $auth->assign($auth->getRole('Client'), $model->id);
- }
- }
- return $this->redirect(['view', 'id' => $model->id]);
- }
- } else {
- $model->password_hash="";
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- }
- public function actionDelete($id)
- {
- ChatGroup::deleteAll(["author_id"=>$id]);
- UsersGroup::deleteAll(["userId"=>$id]);
- Chatteh::deleteAll(["userId"=>$id]);
- Chatteh::deleteAll(["to_user"=>$id]);
- CliUsersActivity::deleteAll(["user_id"=>$id]);
- Project::beforeDeleteUser($id);
- $this->findModel($id)->delete();
- return $this->redirect(['index']);
- }
- public function actionVerifyemail()
- {
- if (isset($_POST["code"])) {
- if (($_POST["code"] !=null)&&($_POST["code"] == Yii::$app->session->get("registcode"))) {
- return 1;
- }else
- return Yii::t("sy/site", "regist_error_message");
- }
- else {
- if(!empty($_POST["email"])) {
- $code = Yii::$app->security->generateRandomString(8);
- Yii::$app->session->set("registcode", $code);
- Yii::$app->mailer->compose()
- ->setFrom("stajor.it@gmail.com")
- ->setTo($_POST["email"])
- ->setSubject('Regist in SYCO')
- ->setTextBody('Date pentru autentificare in portalul Synapsys Community, REGIST CODE:' . $code)
- ->send();
- return Yii::t("sy/user","Verifica cutia postala:") . $_POST["email"];
- }
- else return Yii::t("sy/user","Your must type your email adress!!");
- }
- }
- protected function findModel($id)
- {
- if (($model = User::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
- public function actionResetpassword()
- {
- if(isset($_GET["token"])&&isset($_GET["user"]))
- {
- $control=UserReset::findOne(["user_id"=>$_GET["user"],"token"=>$_GET["token"]]);
- if(isset($control))
- {
- $now = date("Y-m-d H:i:s");
- if((strtotime($now)-strtotime($control->create_datetime))<300) {
- if (isset($_POST["User"])) {
- $user = User::findOne(["id" => Yii::$app->user->id]);
- $user->password_hash = $_POST["User"]["password_hash"];
- $user->save();
- return $this->goHome();
- } else {
- $user = User::findOne(["id" => $_GET["user"]]);
- Yii::$app->user->login($user, null);
- return $this->render("resetPassword");
- }
- }
- else return $this->render("ErrorPage");
- }
- }
- else{
- if(isset($_POST["email"])) {
- $profile=Profile::findOne(["email"=>$_POST["email"]]);
- if(!empty($profile)) {
- $user = User::findOne($profile->user_id);
- $password = Yii::$app->getSecurity()->generateRandomString(64);
- $reset=new UserReset();
- $reset->user_id=$user->id;
- $reset->token=$password;
- $reset->save();
- Yii::$app->mailer->compose()
- ->setFrom("stajor.it@gmail.com")
- ->setTo($_POST["email"])
- ->setSubject('Datele de autentificare prin intermediu SYCO')
- ->setTextBody('Date pentru autentificare in portalul Synapsys Community')
- ->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>')
- ->send();
- return "<h3>".Yii::t("sy/user","Your reset link has been sending on your email,this link is valid 5 min").":".$_POST["email"]."</h3>";
- }
- else
- return "<h3>".Yii::t("sy/user","Email is dont'registed in SYCO")."</h3>";
- }
- }
- }
- public function actionDeletechaths()
- {
- Chatteh::deleteAll(["to_user"=>Yii::$app->user->id]);
- Chatteh::deleteAll(["userId"=>Yii::$app->user->id]);
- return $this->renderAjax('delhistory');
- }
- public function actionNewpassword()
- {
- if(isset($_POST["useroldpassword"])&&isset($_POST["password_hash"]))
- {
- $user=User::findOne(Yii::$app->user->id);
- $oldpass=$_POST["useroldpassword"];
- if($user->validatePassword($oldpass))
- {
- $user->setPassword($_POST["password_hash"]);
- $user->save();
- return $this->renderAjax('success_mess');
- }
- else return "false";
- }
- /*else
- if(isset($_POST["User"]))
- {
- $user=User::findOne(Yii::$app->user->id);
- $oldpass=$_POST["User"]["user-old-password"];
- if($user->validatePassword($oldpass))
- {
- $user->setPassword($_POST["User"]["password_hash"]);
- $user->save();
- return $this->goHome();
- }
- else return false;
- }*/
- else
- {
- return $this->renderAjax('newPassword',["model"=>new User()]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement