/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new User(); if ($model->load(Yii::$app->request->post())) { $model->setPassword('123456'); if ($model->save()) { Yii::$app->session->setFlash('success', 'User berhasil dibuat dengan password 123456'); } else { Yii::$app->session->setFlash('error', 'User gagal dibuat'); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new User(); $authAssignments = AuthAssignment::find()->where(['user_id' => $model->getId()])->column(); $authItems = ArrayHelper::map(AuthItem::find()->where(['type' => 1])->asArray()->all(), 'name', 'name'); $authAssignment = new AuthAssignment(['user_id' => $model->getId()]); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $str = date('ymdhis') . 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' . date('d'); $potong = str_shuffle($str); $random = substr($potong, 3, 16); $model->setPassword($random); if ($model->save()) { $auth = Yii::$app->get('authManager'); $auth->getRolesByUser($model->getId()); $auth->revokeAll($model->getId()); $authorRole = $auth->createRole($_POST['AuthAssignment']['item_name']); $auth->assign($authorRole, $model->getId()); $content = ' <center><img src="http://i.imgur.com/p5lHZXS.png"/></center><br/> <h4 align="center">Badan Pengawas Tenaga Nuklir ' . date('Y') . '</h4> <hr/> <p>Yth ' . $model->username . ',<br/> Dengan ini kami sampaikan akun telah terdaftar untuk masuk ke Sistem Aplikasi Perjalanan Dinas – BAPETEN, sebagai berikut:<br/> Username : '******' <br/> Password :<b>' . $random . '</b><br/> Mohon lakukan penggantian password Anda setelah melakukan login.\\n Terima Kasih. <hr/> <h5 align="center">Subbag Perjalanan Dinas Biro Umum BAPETEN ' . date('Y') . '</h5><br/>'; Yii::$app->mailer->compose("@common/mail/layouts/html", ["content" => $content])->setTo($_POST['User']['email'])->setFrom([$_POST['User']['email'] => $model->username])->setSubject('Ubah Kata Sandi')->setTextBody('12345')->send(); Yii::$app->session->setFlash('success', 'User berhasil dibuat '); } else { Yii::$app->session->setFlash('error', 'User gagal dibuat'); } return $this->redirect(['view', 'id' => $model->getId()]); } else { return $this->render('create', ['model' => $model, 'authAssignment' => $authAssignment, 'authItems' => $authItems]); } }