Exemplo n.º 1
0
 public function search($params)
 {
     $query = static::ITEM_TYPE == static::TYPE_ROLE ? Role::find() : Permission::find();
     $query->joinWith(['group']);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['like', Yii::$app->yee->auth_item_table . '.name', $this->name])->andFilterWhere(['like', Yii::$app->yee->auth_item_table . '.description', $this->description])->andFilterWhere([Yii::$app->yee->auth_item_table . '.group_code' => $this->group_code]);
     return $dataProvider;
 }
Exemplo n.º 2
0
 /**
  * Gather all user permissions and roles and store them in the session
  *
  * @param UserIdentity $identity
  */
 public static function updatePermissions($identity)
 {
     $session = Yii::$app->session;
     // Clear data first in case we want to refresh permissions
     $session->remove(self::SESSION_PREFIX_ROLES);
     $session->remove(self::SESSION_PREFIX_PERMISSIONS);
     $session->remove(self::SESSION_PREFIX_ROUTES);
     // Set permissions last mod time
     $session->set(self::SESSION_PREFIX_LAST_UPDATE, filemtime(self::getPermissionsLastModFile()));
     // Save roles, permissions and routes in session
     $session->set(self::SESSION_PREFIX_ROLES, array_keys(Role::getUserRoles($identity->id)));
     $session->set(self::SESSION_PREFIX_PERMISSIONS, array_keys(Permission::getUserPermissions($identity->id)));
     $session->set(self::SESSION_PREFIX_ROUTES, Route::getUserRoutes($identity->id));
 }
 /**
  * @param int $id - User ID
  *
  * @return \yii\web\Response
  */
 public function actionSetRoles($id)
 {
     if (!Yii::$app->user->isSuperadmin and Yii::$app->user->id == $id) {
         Yii::$app->session->setFlash('error', Yii::t('yee/user', 'You can not change own permissions'));
         return $this->redirect(['set', 'id' => $id]);
     }
     $oldAssignments = array_keys(Role::getUserRoles($id));
     // To be sure that user didn't attempt to assign himself some unavailable roles
     $newAssignments = array_intersect(Role::getAvailableRoles(Yii::$app->user->isSuperAdmin, true), Yii::$app->request->post('roles', []));
     $toAssign = array_diff($newAssignments, $oldAssignments);
     $toRevoke = array_diff($oldAssignments, $newAssignments);
     foreach ($toRevoke as $role) {
         User::revokeRole($id, $role);
     }
     foreach ($toAssign as $role) {
         User::assignRole($id, $role);
     }
     Yii::$app->session->setFlash('crudMessage', Yii::t('yee', 'Saved'));
     return $this->redirect(['set', 'id' => $id]);
 }
Exemplo n.º 4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getRoles()
 {
     return $this->hasMany(Role::className(), ['name' => 'item_name'])->viaTable(Yii::$app->yee->auth_assignment_table, ['user_id' => 'id']);
 }
Exemplo n.º 5
0
                    <strong>
                        <span
                            class="glyphicon glyphicon-th"></span> <?php 
echo Yii::t('yee/user', 'Child roles');
?>
                    </strong>
                </div>
                <div class="panel-body">
                    <?php 
echo Html::beginForm(['set-child-roles', 'id' => $role->name]);
?>

                    <?php 
echo Html::checkboxList('child_roles', ArrayHelper::map($childRoles, 'name', 'name'), ArrayHelper::map($allRoles, 'name', 'description'), ['item' => function ($index, $label, $name, $checked, $value) {
    $list = '<ul style="padding-left: 10px">';
    foreach (Role::getPermissionsByRole($value) as $permissionName => $permissionDescription) {
        $list .= $permissionDescription ? "<li>{$permissionDescription}</li>" : "<li>{$permissionName}</li>";
    }
    $list .= '</ul>';
    $helpIcon = Html::beginTag('span', ['title' => Yii::t('yee/user', 'Permissions for "{role}" role', ['role' => $label]), 'data-content' => $list, 'data-html' => 'true', 'role' => 'button', 'style' => 'margin-bottom: 5px; padding: 0 5px', 'class' => 'btn btn-sm btn-default role-help-btn']);
    $helpIcon .= '?';
    $helpIcon .= Html::endTag('span');
    $checkbox = Html::checkbox($name, $checked, ['label' => $label, 'value' => $value]);
    return "<div><div class='pull-left' style='margin-right: 15px;'>{$checkbox}</div><div>{$helpIcon}</div></div>";
}]);
?>

                    <hr/>
                    <?php 
if (User::hasPermission('manageRolesAndPermissions')) {
    ?>
Exemplo n.º 6
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Role();
     $model->scenario = 'webInput';
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->name]);
     }
     return $this->renderIsAjax('create', compact('model'));
 }
Exemplo n.º 7
0
 /**
  * Default unique validator search only within specific class (Role, Route or Permission) because of the overwritten find() method
  */
 public function validateUniqueName($attribute)
 {
     if (Role::find()->where(['name' => $this->name])->exists()) {
         $this->addError('name', Yii::t('yii', '{attribute} "{value}" has already been taken.', ['attribute' => $this->getAttributeLabel($attribute), 'value' => $this->{$attribute}]));
     }
 }
Exemplo n.º 8
0
                </div>
            </div>

            <?php 
Pjax::begin(['id' => 'user-grid-pjax']);
?>

            <?php 
echo GridView::widget(['id' => 'user-grid', 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'bulkActionOptions' => ['gridId' => 'user-grid'], 'columns' => [['class' => 'yeesoft\\grid\\CheckboxColumn', 'options' => ['style' => 'width:10px']], ['attribute' => 'username', 'controller' => '/user/default', 'class' => 'yeesoft\\grid\\columns\\TitleActionColumn', 'title' => function (User $model) {
    if (User::hasPermission('editUsers')) {
        return Html::a($model->username, ['/user/default/update', 'id' => $model->id], ['data-pjax' => 0]);
    } else {
        return $model->username;
    }
}, 'buttonsTemplate' => '{update} {delete} {permissions} {password}', 'buttons' => ['permissions' => function ($url, $model, $key) {
    return Html::a(Yii::t('yee/user', 'Permissions'), Url::to(['user-permission/set', 'id' => $model->id]), ['title' => Yii::t('yee/user', 'Permissions'), 'data-pjax' => '0']);
}, 'password' => function ($url, $model, $key) {
    return Html::a(Yii::t('yee/user', 'Password'), Url::to(['default/change-password', 'id' => $model->id]), ['title' => Yii::t('yee/user', 'Password'), 'data-pjax' => '0']);
}], 'options' => ['style' => 'width:300px']], ['attribute' => 'email', 'format' => 'raw', 'visible' => User::hasPermission('viewUserEmail')], ['attribute' => 'gridRoleSearch', 'filter' => ArrayHelper::map(Role::getAvailableRoles(Yii::$app->user->isSuperAdmin), 'name', 'description'), 'value' => function (User $model) {
    return implode(', ', ArrayHelper::map($model->roles, 'name', 'description'));
}, 'format' => 'raw', 'visible' => User::hasPermission('viewUserRoles')], ['class' => 'yeesoft\\grid\\columns\\StatusColumn', 'attribute' => 'superadmin', 'visible' => Yii::$app->user->isSuperadmin, 'options' => ['style' => 'width:60px']], ['class' => 'yeesoft\\grid\\columns\\StatusColumn', 'attribute' => 'status', 'optionsArray' => [[User::STATUS_ACTIVE, Yii::t('yee', 'Active'), 'primary'], [User::STATUS_INACTIVE, Yii::t('yee', 'Inactive'), 'info'], [User::STATUS_BANNED, Yii::t('yee', 'Banned'), 'default']], 'options' => ['style' => 'width:60px']]]]);
?>

            <?php 
Pjax::end();
?>

        </div>
    </div>
</div>