public static function getRoles() { $condition = ''; if (\Yii::$app->user->identity->role != 'Superadmin') { $condition = ' AND name != "Superadmin"'; } return AuthItem::find()->where('type =:type' . $condition, [':type' => self::TYPE_ROLE])->all(); }
/** * 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(['like', 'name', $this->name])->andFilterWhere(['type' => 1])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['not like', 'name', '/']); return $dataProvider; }
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]); }
/** * 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, 'sort' => ['defaultOrder' => ['type' => SORT_ASC]]]); $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; }
use backend\models\User; // Find all users first, else it will stop the script if (!empty($getAllUsers = User::find()->all())) { // Find all auth items first, else it will stop the script if (!empty($getAllAuthItems = AuthItem::find()->all())) { foreach ($getAllAuthItems as $authItems) { $allAuthItems[] = $authItems->name; } } else { exit("No 'auth item' data in '" . AuthItem::tableName() . "' table." . PHP_EOL); } // Check if 'admin' exists, else stop the script if (in_array('admin', $allAuthItems)) { $getAdminOnly = array_search('admin', $allAuthItems); } else { exit("Create an 'admin' first in " . AuthItem::tableName() . " table" . PHP_EOL); } // Random created/updated dates $created = $faker->dateTimeBetween('-7 days', 'now')->format('Y-m-d H:i:s'); foreach ($getAllUsers as $user) { // Well, search me first before anything else :D if ($user->username === 'vrigzalejo') { $seedAuthAssignment[] = ['item_name' => $allAuthItems[$getAdminOnly], 'user_id' => $user->id, 'created_at' => $created]; } else { $seedAuthAssignment[] = ['item_name' => $allAuthItems[array_rand($allAuthItems)], 'user_id' => $user->id, 'created_at' => $created]; } } // Database seed the 'auth_assignment' table according to users number if ($index < count($getAllUsers)) { return $seedAuthAssignment[$index]; } else {
public static function createCachedRolesGroup() { $auth = \Yii::$app->authManager; $newLine = "\r\n"; $content = '<?php' . $newLine; $groups = $auth->getChildren('root_role'); foreach ($groups as $group) { $content .= '$cachedRolesGroup[\'' . $group->name . '\'] = [' . $newLine; $content .= AuthItem::createItem($group); $content .= "];" . $newLine; } $dataRoot = \Yii::getAlias('@data'); TFileHelper::writeFile([$dataRoot, 'cache', 'cachedRolesGroup.php'], $content); }
use yii\widgets\ActiveForm; use backend\models\AuthItem; /* @var $this yii\web\View */ /* @var $model backend\models\AuthItem */ /* @var $form yii\widgets\ActiveForm */ ?> <?php $form = ActiveForm::begin(); ?> <div class="box-body auth-item-form"> <?php echo $form->field($model, 'name')->textInput(['maxlength' => true, 'placeholder' => '输入' . $model->getAttributeLabel('name')]); ?> <?php echo $form->field($model, 'type')->textInput()->dropDownList(AuthItem::getType(), array('onchange' => 'showit(' . yii\rbac\Item::TYPE_PERMISSION . ',"parent-authitem-name-div",this);')); ?> <div class="form-group field-parent-authitem-name required" style="display:none;" id='parent-authitem-name-div'> <label for="parent-authitem-name" class="control-label">父级标记</label> <input type="text" placeholder="请输入父级标记" maxlength="64" name="parent_auth_item" class="form-control" id="parent-authitem-name"> </div> <?php echo $form->field($model, 'description')->textarea(['rows' => 6]); ?> </div> <div class="box-footer"> <?php echo Html::submitButton($model->isNewRecord ? '创建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php
/* @var $this yii\web\View */ /* @var $model backend\models\AuthItemChild */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="auth-item-child-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'parent')->dropDownList(ArrayHelper::map(AuthItem::find()->all(), 'name', 'name'), ['prompt' => 'Select parent permission']); ?> <?php echo $form->field($model, 'child')->dropDownList(ArrayHelper::map(AuthItem::find()->all(), 'name', 'name'), ['prompt' => 'Select child permission']); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getChild0() { return $this->hasOne(AuthItem::className(), ['name' => 'child']); }
/** * @return \yii\db\ActiveQuery */ public function getItemNames() { return $this->hasMany(AuthItem::className(), ['name' => 'item_name'])->viaTable('auth_assignment', ['user_id' => 'id']); }
/* @var $this yii\web\View */ /* @var $model backend\models\AuthItemChild */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="auth-item-child-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'parent')->widget(Select2::classname(), ['data' => ArrayHelper::map(AuthItem::find()->where(['and', "type=1"])->all(), 'name', 'name'), 'language' => 'en', 'options' => ['placeholder' => 'Please Select']]); ?> <?php echo $form->field($model, 'child')->widget(Select2::classname(), ['data' => ArrayHelper::map(AuthItem::find()->where(['and', "type=2"])->all(), 'name', 'name'), 'language' => 'en', 'options' => ['placeholder' => 'Please Select']]); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getAuthItems() { return $this->hasMany(AuthItem::className(), ['rule_name' => 'name']); }
protected function findModel($id) { if (($model = AuthItem::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
<?php echo $form->field($model, 'newsletter', ['template' => "{label}{input}\n{hint}\n{error}"])->dropDownList([1 => 'Ya', 0 => 'Tidak'], []); ?> <?php echo $form->field($model, 'status', ['template' => "{label}{input}\n{hint}\n{error}"])->dropDownList(User::getStatus(), ['prompt' => '-- Pilih --']); ?> <?php // $form->field($model, 'role',[ // 'template' => "{label}{input}\n{hint}\n{error}" // ])->dropDownList([1 => 'Admin', 0 => 'Biasa'], [ // 'prompt' => '-- Pilih --' // ]); ?> <?php echo $form->field($model, 'role', ['template' => "{label}{input}\n{hint}\n{error}"])->dropDownList(ArrayHelper::map(AuthItem::getRoles(), 'name', 'name'), ['prompt' => '-- Pilih --']); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getNames0() { return $this->hasMany(AuthItem::className(), ['name' => 'parent'])->viaTable('auth_item_child', ['child' => 'name']); }
$this->title = '操作类型列表'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="col-xs-12"> <div class="box box-primary"> <div class="box-header with-border"> <?php echo $this->render('_search', ['model' => $searchModel]); ?> </div> <div class="box-body no-padding"> <?php echo GridView::widget(['layout' => '<div class="mailbox-controls">' . Html::a('<i class="fa fa-plus"></i>新建', ['create'], ['class' => 'btn btn-default btn-sm']) . Html::button('<i class="fa fa-refresh"></i>同步', ['class' => 'btn btn-default btn-sm button-ajax-loading', 'href' => Yii::$app->urlManager->createUrl(['auth-item/list-action']), 'go_url' => '.', 'data-loading-text' => '...']) . Html::button('<i class="fa fa-trash-o"></i>删除所有', ['class' => 'btn btn-default btn-sm button-ajax-loading', 'href' => Yii::$app->urlManager->createUrl(['auth-item/clear-permission']), 'go_url' => '.', 'data-loading-text' => '...']) . '<div class="pull-right">{summary}</div></div>' . "\n{items}\n{pager}", 'dataProvider' => $dataProvider, 'columns' => [['attribute' => 'name', 'value' => function ($data) { return $data->description . '(' . $data->name . ')'; }, 'enableSorting' => false], ['attribute' => 'type', 'value' => function ($data) { return AuthItem::getType()[$data->type]; }], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{member} {auth} {delete}', 'buttons' => ['member' => function ($url, $model) { return $model->type == Item::TYPE_ROLE ? Html::a('<span class="fa fa-user"></span>', $url, ['title' => '设置组员', 'data-target' => "#authItemChooseMemberModal", 'data-toggle' => "modal"]) : ''; }, 'auth' => function ($url, $model) { return $model->type == Item::TYPE_ROLE ? Html::a('<span class="fa fa-key"></span>', $url, ['title' => '分配权限', 'data-target' => "#authItemAuthModal", 'data-toggle' => "modal"]) : ''; }]]]]); ?> </div> </div> </div> <!-- Modal --> <div class="modal fade" id="authItemChooseMemberModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" > <div class="modal-dialog" style="width: 90%;" role="document"> <div class="modal-content"></div> </div> </div>
/** * 给角色分配权限 * @return string */ public function actionAssignauth() { if (Yii::$app->request->isPost) { $posts = Yii::$app->request->post(); $auth = Yii::$app->authManager; $role = $auth->getRole($posts['rolename']); $thismenu = TMenu::findOne($posts['menuid']); $route = $thismenu->route; $permission = $auth->getPermission($route); if ($posts['ck'] == 'true') { if ($posts['level'] == 3) { //2级菜单 $father = $thismenu->father; $fpermission = $auth->getPermission($father->route); $this->addChild($role, $fpermission); //1级菜单 $this->addChild($role, $auth->getPermission($father->father->route)); } if ($posts['level'] == 2) { //1级菜单 $fpermission = $auth->getPermission($thismenu->father->route); $this->addChild($role, $fpermission); //3级菜单 $children = $thismenu->son; foreach ($children as $son) { $this->addChild($role, $auth->getPermission($son->route)); } } if ($posts['level'] == 1) { //子子孙孙都加权限 $sons = $thismenu->son; foreach ($sons as $son) { $this->addChild($role, $auth->getPermission($son->route)); if ($son->level == 2) { $gsons = $son->son; foreach ($gsons as $gson) { $this->addChild($role, $auth->getPermission($gson->route)); } } } } //自身加入权限 $auth->addChild($role, $permission); } else { if ($posts['level'] == 3 && $posts['cntlv3'] == 0) { $father = $thismenu->father; $auth->removeChild($role, $auth->getPermission($father->route)); if ($posts['cntlv3'] == 0) { $auth->removeChild($role, $auth->getPermission($father->route)); } if ($posts['cntlv2'] == 0) { $auth->removeChild($role, $auth->getPermission($father->father->route)); } } if ($posts['level'] == 2) { foreach ($thismenu->son as $son) { $auth->removeChild($role, $auth->getPermission($son->route)); } if ($posts['cntlv2'] == 0) { $auth->removeChild($role, $auth->getPermission($thismenu->father->route)); } } if ($posts['level'] == 1) { foreach ($thismenu->son as $son) { $auth->removeChild($role, $auth->getPermission($son->route)); foreach ($son->son as $gson) { $auth->removeChild($role, $auth->getPermission($gson->route)); } } } //删除自身 $auth->removeChild($role, $permission); } } $list = TMenu::find()->where('level=1')->all(); $rolename = Yii::$app->request->get('rolename'); $model = AuthItem::findOne($rolename); return $this->render('assignauth', ['list' => $list, 'rolename' => $rolename, 'role' => Yii::$app->authManager->getRole($rolename), 'model' => $model]); }
/** * @return \yii\db\ActiveQuery */ public function getItemName() { return $this->hasOne(AuthItem::className(), ['name' => 'item_name']); }
<?php /** * Created by PhpStorm. * User: vrigzlinuxmint13 * Date: 9/6/15 * Time: 1:58 PM */ use backend\models\AuthItem; // Search for 'admin' name in auth_item table first if (!empty($authItem = AuthItem::find()->where(['name' => 'admin'])->one())) { // Find all auth_items except 'admin' foreach ($allAuthItems = AuthItem::find()->all() as $aItem) { if ($aItem->name !== 'admin') { $authItemChildData[] = ['parent' => $authItem->name, 'child' => $aItem->name]; } } // Db seed auth_item_child according to parent-child relationship if ($index < count($allAuthItems) - 1) { return $authItemChildData[$index]; } else { exit("Please set '" . preg_replace('/\\.php$/', '', basename(__FILE__)) . "' --count from 1 up to " . count($authItemChildData) . PHP_EOL); } } else { exit("No 'admin' data in '" . AuthItem::tableName() . "' table." . PHP_EOL); }
/** * 授权 * @return [type] [description] */ public function actionAuth() { $this->layout = false; $auth = Yii::$app->authManager; if (Yii::$app->request->post()) { $zu_id = Yii::$app->request->post('item_zu_id'); $zu_model = $this->findModel($zu_id); $item_ids = Yii::$app->request->post('item_id'); foreach ($item_ids as $key => $one_item) { //父类直接删除 if (count(explode('/', $one_item)) == 3) { unset($item_ids[$key]); } } $children = $auth->getChildren($zu_id); foreach ($children as $son) { if (!in_array($son->name, $item_ids)) { $auth->removeChild($zu_model, $son); } } foreach ($item_ids as $item_id) { $item_obj = $this->findModel($item_id); if (!$auth->hasChild($zu_model, $item_obj)) { $auth->addChild($zu_model, $item_obj); } } return json_encode(['status' => 1]); } $zu_id = Yii::$app->request->get('id'); $zu_model = $this->findModel($zu_id); //所有的操作 $list = AuthItem::find()->where(['type' => '2'])->all(); //组装新数组 $new_list = array(); $key_arr = array(); $is_have_arr = []; foreach ($list as $row) { $is_have_arr[$row->name] = $auth->hasChild($zu_model, $row); $key_arr[$row->name] = $row; } $child_arr = []; foreach ($list as $row) { $child = $auth->getChildren($row->name); if ($child) { foreach ($child as $key => $value) { $child_arr[$row->name][] = $key_arr[$key]; } $new_list[] = $row; } } return $this->render('auth', array('list' => $new_list, 'item_zu_id' => $zu_id, 'is_have_arr' => $is_have_arr, 'child_arr' => $child_arr)); }
echo $form->field($model, 'password_reset_token')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'status')->textInput(); ?> <?php echo $form->field($model, 'created_at')->textInput(); ?> <?php echo $form->field($model, 'updated_at')->textInput(); ?> <?php echo $form->field($model, 'auth_assignment')->dropDownList(ArrayHelper::map(\backend\models\AuthItem::find()->all(), 'name', 'name'), ['prompt' => 'Select Role']); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>