/** * Creates a new AuthItem model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new AuthItem(null); $model->type = $this->type; if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) { Helper::invalidate(); return $this->redirect(['view', 'id' => $model->name]); } else { return $this->render('create', ['model' => $model]); } }
public function search($params) { $query = AuthItem::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { 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; }
/** * 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; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $this->scenario = 'search'; $query = AuthItem::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => array('pageSize' => 100)]); $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]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'id', $this->id])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'rule_name', $this->rule_name])->andFilterWhere(['like', 'data', $this->data]); return $dataProvider; }
public function actionInit() { $auth = \Yii::$app->authManager; $auth->removeAll(); //$arrPerm = []; $query = AuthItem::find()->where(['type' => 0]); $modelAuthItem = $query->all(); foreach ($modelAuthItem as $obj) { $arrPerm[$obj->name] = $obj->description; } //$arrRole = []; $query = AuthItem::find()->where(['type' => 2]); $modelAuthItem = $query->all(); foreach ($modelAuthItem as $obj) { $arrRole[$obj->name] = $obj->description; } $query = AuthItem::find()->where(['type' => 2]); $modelAuthItem = $query->all(); //$arrRolePerm = []; foreach ($modelAuthItem as $obj) { $name = $obj->name; $query = AuthItemChild::find()->where(['parent' => $name]); $modelAuthItemChild = $query->all(); foreach ($modelAuthItemChild as $objchild) { $arrRolePerm[$name][] = $objchild->child; } } $auth->removeAll(); foreach ($arrPerm as $permName => $title) { $perm = $auth->createPermission($permName); $perm->description = $title; $auth->add($perm); } foreach ($arrRole as $roleName => $title) { $role = $auth->createRole($roleName); $role->description = $title; $auth->add($role); // assign role permission foreach ($arrRolePerm[$roleName] as $permName) { $perm = $auth->getPermission($permName); $auth->addChild($role, $perm); } } }
/** * @return \yii\db\ActiveQuery */ public function getAuthItems() { return $this->hasMany(AuthItem::className(), ['rule_name' => 'name']); }
/** * @return \yii\db\ActiveQuery */ public function getChild0() { return $this->hasOne(AuthItem::className(), ['name' => 'child']); }
/** * @inheritdoc */ public function attributeLabels() { $attributeLabels = parent::attributeLabels(); $attributeLabels['parent'] = '上级角色'; return $attributeLabels; }
<?php echo $form->field($model, 'first_name')->textInput(); ?> <?php echo $form->field($model, 'middle_name')->textInput(); ?> <?php echo $form->field($model, 'last_name')->textInput(); ?> <?php echo $form->field($model, 'authAssignments')->widget(MultipleInput::className(), ['min' => 1, 'limit' => 10, 'columns' => [['name' => 'item_name', 'type' => 'dropDownList', 'defaultValue' => 'user', 'items' => ArrayHelper::map(\common\models\AuthItem::find()->all(), 'name', 'description')]]]); ?> <?php $tzlist = DateTimeZone::listIdentifiers(DateTimeZone::ALL); $list = array(); foreach (Yii::$app->params['timezones'] as $key => $tz) { $list[$tz] = $key; } ?> <?php echo $form->field($model, 'timezone')->dropDownList($list); ?> <div class="form-group">
/** * Finds the AuthItem model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return AuthItem the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = AuthItem::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @param int $type * @param bool $empty * @return array * Получение массива ролей (type=1) и точек доступа (type=2) */ public static function getAll($type = null, $key = 'name') { $roles = []; if ($type) { $role = AuthItem::find()->where(['type' => $type])->all(); if ($role) { foreach ($role as $r) { if ($key == 'name') { $roles[$r->name] = $r->name; } elseif ($key == 'id') { $roles[$r->id] = $r->name; } } } } else { $role = AuthItem::find()->all(); if ($role) { foreach ($role as $r) { if ($r->type == 1) { if ($key == 'name') { $roles['Роли'][$r->name] = $r->name; } elseif ($key == 'id') { $roles['Роли'][$r->id] = $r->name; } } else { if ($key == 'name') { $roles['Точки доступа'][$r->name] = $r->name; } elseif ($key == 'id') { $roles['Точки доступа'][$r->id] = $r->name; } } } } } return $roles; }
if (!$model->isNewRecord) { echo Html::a('<span class="glyphicon glyphicon-trash"></span> Удалить', ['delete', 'id' => $model->id], ['class' => 'btn btn-danger']); } ?> </div> </div> <div class="row"> <div class="col-sm-6"> <?php echo $form->field($model, 'name')->textInput(['maxlength' => true]); ?> </div> <div class="col-sm-6"> <?php echo $form->field($model, 'type')->dropDownList(AuthItem::getTypes()); ?> </div> </div> <div class="row"> <div class="col-sm-12"> <?php echo $form->field($model, 'description')->textarea(['rows' => 6]); ?> </div> </div> <?php ActiveForm::end(); ?>
?> <div class="user-index"> <?php echo $this->render('@app/views/site/_alert'); ?> <?php $gridColumns = [['class' => 'kartik\\grid\\SerialColumn', 'contentOptions' => ['class' => 'kartik-sheet-style'], 'width' => '30px', 'header' => '', 'headerOptions' => ['class' => 'kartik-sheet-style']], ['class' => 'kartik\\grid\\ExpandRowColumn', 'width' => '50px', 'value' => function () { return GridView::ROW_COLLAPSED; }, 'detail' => function ($model) { return Yii::$app->controller->renderPartial('_view', ['model' => $model]); }, 'headerOptions' => ['class' => 'kartik-sheet-style']], ['attribute' => 'id', 'width' => '70px'], ['attribute' => 'role_id', 'value' => function ($model) { return isset($model->role) ? $model->role->name : ''; }, 'filter' => AuthItem::getAll(1), 'filterWidgetOptions' => ['pluginOptions' => ['allowClear' => true]], 'filterInputOptions' => ['placeholder' => '', 'class' => 'form-control'], 'format' => 'raw'], ['attribute' => 'first_name', 'format' => 'html', 'value' => function ($model) { return Html::a($model->first_name, ['/user/update', 'id' => $model->id]); }], 'last_name', 'email:email', 'phone', ['attribute' => 'created_at', 'value' => function ($model) { return CFF::FormatData($model->created_at, true); }, 'width' => '200px', 'filter' => DatePicker::widget(['value' => isset($_GET['UserSearch']['created_at']) ? $_GET['UserSearch']['created_at'] : '', 'name' => 'UserSearch[created_at]', 'type' => DatePicker::TYPE_COMPONENT_APPEND, 'pluginOptions' => ['format' => 'dd.mm.yyyy', 'todayHighlight' => true]])], ['attribute' => 'status', 'vAlign' => 'middle', 'format' => 'raw', 'value' => function ($model) { switch ($model->status) { case User::STATUS_BLOCKED: return '<span class="label label-danger"> <i class="glyphicon glyphicon-lock"></i> Заблокирован</span>'; break; case User::STATUS_WAIT: return '<span class="label label-warning"> <i class="glyphicon glyphicon-hourglass"></i> Не активен</span>'; break; case User::STATUS_ACTIVE: return '<span class="label label-success">
<?php $birthday = $model->birthday ? $model->birthday : null; ?> <?php echo DatePicker::widget(['name' => 'User[birthday]', 'value' => $birthday, 'type' => DatePicker::TYPE_COMPONENT_APPEND, 'options' => ['placeholder' => ''], 'pluginOptions' => ['format' => 'dd.mm.yyyy', 'todayHighlight' => true]]); ?> </div> </div> </div> <div class="row"> <div class="col-sm-6"> <?php echo $form->field($model, 'role_id')->dropDownList(AuthItem::getAll(1, 'id')); ?> </div> <div class="col-sm-6"> <?php echo $form->field($model, 'status')->dropDownList(User::getStatusesArray()); ?> </div> </div> <div class="row"> <div class="col-sm-6"> <?php echo $form->field($model, 'oauth_fb_id')->textInput(['maxlength' => true]); ?> </div>
public static function createCachedPermissions() { $auth = \Yii::$app->authManager; $newLine = "\r\n"; $content = '<?php' . $newLine; $permissionContent = ''; $categories = $auth->getChildren('root_permission'); foreach ($categories as $category) { $content .= '$cachedPermissionCategories[\'' . $category->name . '\'] = [' . $newLine; $content .= AuthItem::createItem($category); $content .= "\t'permissions' => [" . $newLine; $permissions = $auth->getChildren($category->name); foreach ($permissions as $permission) { $content .= "\t\t'" . $permission->name . "'," . $newLine; $permissionContent .= '$cachedPermissions[\'' . $permission->name . '\'] = [' . $newLine; $permissionContent .= AuthItem::getCacheItemValue('category', $category->name); $permissionContent .= AuthItem::createItem($permission); $permissionContent .= "];" . $newLine; } $content .= ' ],' . $newLine; $content .= "];" . $newLine; } $content .= $permissionContent; $dataRoot = \Yii::getAlias('@data'); TFileHelper::writeFile([$dataRoot, 'cache', 'cachedPermissions.php'], $content); }
public function actionList() { $query = User::find(); if (\Yii::$app->request->isPost) { if ($_REQUEST['op'] == "delete") { $arrId = $_REQUEST['selectUser']; foreach ($arrId as $lst) { $queryUser = User::find(); $User = $queryUser->where(['id' => $lst])->one()->delete(); } echo $this->redirect(Url::toRoute('user/list')); } if ($_REQUEST['op'] == "search") { $searchText = $_REQUEST['searchText']; $query->andWhere(['LIKE', 'firstName', '%' . $searchText . '%', false]); } } $pagination = new Pagination(['defaultPageSize' => 20, 'totalCount' => $query->count()]); $queryauth = AuthItem::find(); $queryauth->where(['type' => '2']); $modelauth = $queryauth->all(); $arrauth = ''; foreach ($modelauth as $lst) { $arrauth[0] = 'เลือกสิทธิ์ ให้กับ user'; $arrauth[] = $lst->name; } $model = $query->orderBy('createTime DESC')->offset($pagination->offset)->limit($pagination->limit)->all(); echo $this->render('list', ['model' => $model, 'arrauth' => $arrauth, 'pagination' => $pagination]); }
/** * @return \yii\db\ActiveQuery */ public function getParents() { return $this->hasMany(AuthItem::className(), ['name' => 'parent'])->viaTable('auth_item_child', ['child' => 'name']); }
/** * Signs user up. * * @return mixed */ public function actionSignup() { $model = new SignupForm(); $authItems = AuthItem::find()->all(); if ($model->load(Yii::$app->request->post())) { if ($user = $model->signup()) { if (Yii::$app->getUser()->login($user)) { return $this->goHome(); } } } return $this->render('signup', ['model' => $model, 'authItems' => $authItems]); }
/** * @return \yii\db\ActiveQuery */ public function getItemNames() { return $this->hasMany(AuthItem::className(), ['name' => 'item_name'])->viaTable('auth_assignment', ['user_id' => 'id']); }
public function getdropItem() { $data = AuthItem::find()->where(['type' => 2])->asArray()->all(); return ArrayHelper::map($data, 'name', 'name'); }
$(".delete-all").click(function(){ var keys = $(".grid-view").yiiGridView("getSelectedRows"); $.ajax({ url: "/admin/authitemchild/multidelete", type:"POST", data:{keys: keys}, success: function(data){ location.reload(); } }); }); '); ?> <div class="role-index"> <?php echo $this->render('@app/views/site/_alert'); ?> <?php $gridColumns = [['class' => 'kartik\\grid\\SerialColumn', 'contentOptions' => ['class' => 'kartik-sheet-style'], 'width' => '30px', 'header' => '', 'headerOptions' => ['class' => 'kartik-sheet-style']], ['attribute' => 'parent', 'value' => function ($model) { return $model->parent; }, 'filterType' => GridView::FILTER_SELECT2, 'filter' => AuthItem::getAll(1), 'filterWidgetOptions' => ['pluginOptions' => ['allowClear' => true]], 'filterInputOptions' => ['placeholder' => ' ', 'class' => 'form-control'], 'format' => 'raw'], ['attribute' => 'child', 'value' => function ($model) { return Html::a($model->child, ['/permission/update', 'id' => $model->id]); }, 'filterType' => GridView::FILTER_SELECT2, 'filter' => AuthItem::getAll(), 'filterWidgetOptions' => ['pluginOptions' => ['allowClear' => true]], 'filterInputOptions' => ['placeholder' => ' ', 'class' => 'form-control'], 'format' => 'raw'], ['class' => 'kartik\\grid\\ActionColumn', 'template' => '{update} {delete}'], ['class' => 'kartik\\grid\\CheckboxColumn', 'headerOptions' => ['class' => 'kartik-sheet-style']]]; echo GridView::widget(['layout' => "{items}\n{summary}\n{pager}", 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => $gridColumns, 'containerOptions' => ['style' => 'overflow: auto'], 'headerRowOptions' => ['class' => 'kartik-sheet-style'], 'filterRowOptions' => ['class' => 'kartik-sheet-style'], 'panel' => ['heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-eye-close"></i> Допуски</h3>', 'type' => GridView::TYPE_PRIMARY, 'before' => Html::a('<span class="glyphicon glyphicon-plus"></span> Создать', ['create'], ['class' => 'btn btn-success']), 'after' => "<div class='text-right'><b>Выбранные:</b> " . Html::button('<span class="glyphicon glyphicon-trash"></span> Удалить', ['class' => 'btn btn-danger delete-all']) . "</div>"], 'export' => ['fontAwesome' => true], 'bordered' => true, 'striped' => true, 'condensed' => true, 'persistResize' => false, 'hover' => true, 'responsive' => true]); ?> </div>
<div class="col-sm-12"> <?php echo Html::submitButton('<span class="glyphicon glyphicon-floppy-disk"></span> Сохранить', ['class' => 'btn btn-primary']); ?> <?php if (!$model->isNewRecord) { echo Html::a('<span class="glyphicon glyphicon-trash"></span> Удалить', ['delete', 'id' => $model->id], ['class' => 'btn btn-danger']); } ?> </div> </div> <div class="row"> <div class="col-sm-6"> <?php echo $form->field($model, 'parent')->widget(Select2::classname(), ['data' => AuthItem::getAll(1), 'options' => ['placeholder' => ' '], 'pluginOptions' => ['allowClear' => true]]); ?> </div> <div class="col-sm-6"> <?php echo $form->field($model, 'child')->widget(Select2::classname(), ['data' => AuthItem::getAll(), 'options' => ['placeholder' => ' '], 'pluginOptions' => ['allowClear' => true]]); ?> </div> </div> <?php ActiveForm::end(); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getItemName() { return $this->hasOne(AuthItem::className(), ['name' => 'item_name']); }
public function authItem() { $data = ArrayHelper::map(AuthItem::find()->where(['type' => 1])->all(), 'name', 'name'); return $data; }
/** * Finds the AuthItem model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return AuthItem the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = AuthItem::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('Страница не найдена.'); } }
/** * @inheritdoc * Добавление связи роль=>пользователь в таблицу auth_assignment (RBAC) */ public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); AuthAssignment::deleteAll(['user_id' => $this->id]); /** @var \common\models\AuthItem $role */ $role = AuthItem::findOne($this->role_id); if ($role) { $assign = new AuthAssignment(); $assign->user_id = (string) $this->id; $assign->item_name = $role->name; $assign->created_at = time(); $assign->save(); } return true; }