/** * Method CheckRoute is used for checking if route right to access * * if ((Mimin::checkRoute($this->context->id.'/create'))){ * echo Html::a('Create Foo', ['create'], ['class' => 'btn btn-success']); * } * * @param $route * @param bool $strict * @return bool */ public static function checkRoute($route, $strict = false) { $user = Yii::$app->user; $permission = substr($route, 0, 1) == '/' ? $route : '/' . $route; if ($user->can($permission)) { return true; } if (!$strict) { $pos = strrpos($permission, '/'); $parent = substr($permission, 0, $pos); $authItems = AuthItem::find()->where(['like', 'name', $parent])->all(); foreach ($authItems as $authItem) { $permission = $authItem->name; if ($user->can($permission)) { return true; } } } $allowActions = Yii::$app->allowActions; foreach ($allowActions as $action) { $action = substr($action, 0, 1) == '/' ? $action : '/' . $action; if ($action === '*' or $action === '*/*') { return true; } else { if (substr($action, -1) === '*') { $length = strlen($action) - 1; return substr($action, 0, $length) == substr($route, 0, $length); } else { return $action == $route; } } } return false; }
/** * Displays a single User model. * @param integer $id * @return mixed */ public function actionView($id) { $model = $this->findModel($id); $authAssignments = AuthAssignment::find()->where(['user_id' => $model->id])->column(); $authItems = ArrayHelper::map(AuthItem::find()->where(['type' => 1])->asArray()->all(), 'name', 'name'); $authAssignment = new AuthAssignment(['user_id' => $model->id]); if (Yii::$app->request->post()) { $authAssignment->load(Yii::$app->request->post()); // delete all role AuthAssignment::deleteAll(['user_id' => $model->id]); if (is_array($authAssignment->item_name)) { foreach ($authAssignment->item_name as $item) { if (!in_array($item, $authAssignments)) { $authAssignment2 = new AuthAssignment(['user_id' => $model->id]); $authAssignment2->item_name = $item; $authAssignment2->created_at = time(); $authAssignment2->save(); $authAssignments = AuthAssignment::find()->where(['user_id' => $model->id])->column(); } } } Yii::$app->session->setFlash('success', 'Data tersimpan'); } $authAssignment->item_name = $authAssignments; return $this->render('view', ['model' => $model, 'authAssignment' => $authAssignment, 'authItems' => $authItems]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = AuthItem::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['type' => $this->type, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'rule_name', $this->rule_name])->andFilterWhere(['like', 'data', $this->data]); return $dataProvider; }
/** * @inheritdoc * $items=[ * ['label' => 'User', 'url' => ['/mimin/user']], * ['label' => 'Role', 'url' => ['/mimin/role']], * ['label' => 'Route', 'url' => ['/mimin/route']], * ]; * $items = Mimin::filterRouteMenu($items); * if(count($items)>0){ * $menuItems[] = ['label' => 'Administrator', 'items' => $items]; * } */ public static function filterRoute($route, $strict = false) { $allowedRoutes = []; $user = Yii::$app->user; $permission = substr($route, 0, 1) == '/' ? $route : '/' . $route; if ($user->can($permission)) { return true; } if (!$strict) { $pos = strrpos($permission, '/'); $parent = substr($permission, 0, $pos); $authItems = AuthItem::find()->where(['like', 'name', $parent])->all(); foreach ($authItems as $authItem) { $permission = $authItem->name; if ($user->can($permission)) { return true; } } foreach (Yii::$app->allowActions as $wilcard) { $wilcard = substr($wilcard, 0, 1) == '/' ? $wilcard : '/' . $wilcard; // * if ($wilcard === '/*') { return true; } // anything/* or anything/anything/* $pos = strrpos($wilcard, '/'); $first = substr($wilcard, 0, $pos); $second = substr($wilcard, $pos, strlen($wilcard) - $pos); if ($second === '/*') { $pos2 = strrpos($permission, '/'); $first2 = substr($permission, 0, $pos2); if ($first2 === $first) { return true; } } if ($wilcard === $permission) { return true; } } } return false; }
/** * @inheritdoc * $items=[ * ['label' => 'User', 'url' => ['/mimin/user']], * ['label' => 'Role', 'url' => ['/mimin/role']], * ['label' => 'Route', 'url' => ['/mimin/route']], * ]; * $items = Mimin::filterRouteMenu($items); * if(count($items)>0){ * $menuItems[] = ['label' => 'Administrator', 'items' => $items]; * } */ public static function filterRoute($route, $strict = false) { $allowedRoutes = []; $user = Yii::$app->user; $permission = substr($route, 0, 1) == '/' ? $route : '/' . $route; if ($user->can($permission)) { return true; } if (!$strict) { $pos = strrpos($permission, '/'); $parent = substr($permission, 0, $pos); $authItems = AuthItem::find()->where(['like', 'name', $parent])->all(); foreach ($authItems as $authItem) { $permission = $authItem->name; if ($user->can($permission)) { return true; } } } return false; }
/** * 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]); } }