public function safeDown()
 {
     Permission::deleteAll(['name' => ['viewUsers', 'createUsers', 'editUsers', 'deleteUsers', 'changeUserPassword', 'assignRolesToUsers', 'viewVisitLog', 'viewUserRoles', 'viewRegistrationIp', 'viewUserEmail', 'editUserEmail', 'bindUserToIp']]);
     Permission::deleteAll(['name' => ['changeOwnPassword']]);
     Role::deleteIfExists(['name' => 'Admin']);
     AuthItemGroup::deleteAll(['code' => ['userManagement', 'userCommonPermissions']]);
 }
 public function safeUp()
 {
     Route::refreshRoutes();
     (new AuthItemGroup(['name' => 'Settings', 'code' => 'settings']))->save();
     Role::assignRoutesViaPermission('Admin', 'fullAccessToSettings', ['settings/manage/*'], null, 'settings');
     Role::assignRoutesViaPermission('SeoManager', 'accessToSeoSettings', ['settings/manage/seo'], 'Access to SEO settings', 'settings');
     AuthHelper::invalidatePermissions();
 }
 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->getModule(\Yii::$app->user->moduleAliasName)->auth_item_table . '.name', $this->name])->andFilterWhere(['like', Yii::$app->getModule(\Yii::$app->user->moduleAliasName)->auth_item_table . '.description', $this->description])->andFilterWhere([Yii::$app->getModule(\Yii::$app->user->moduleAliasName)->auth_item_table . '.group_code' => $this->group_code]);
     return $dataProvider;
 }
Ejemplo n.º 4
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));
 }
Ejemplo n.º 5
0
 /**
  * @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', UserManagementModule::t('back', '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('success', UserManagementModule::t('back', 'Saved'));
     return $this->redirect(['set', 'id' => $id]);
 }
 /**
  * @param string $id
  *
  * @return string
  */
 public function actionView($id)
 {
     $item = $this->findModel($id);
     $routes = Route::find()->asArray()->all();
     $permissions = Permission::find()->andWhere(['not in', Yii::$app->getModule('user-management')->auth_item_table . '.name', [Yii::$app->getModule('user-management')->commonPermissionName, $id]])->joinWith('group')->all();
     $permissionsByGroup = [];
     foreach ($permissions as $permission) {
         $permissionsByGroup[@$permission->group->name][] = $permission;
     }
     $childRoutes = AuthHelper::getChildrenByType($item->name, AbstractItem::TYPE_ROUTE);
     $childPermissions = AuthHelper::getChildrenByType($item->name, AbstractItem::TYPE_PERMISSION);
     $allRoles = Role::find()->all();
     $roles = array();
     foreach ($allRoles as $role) {
         $rolePermissions = $role->getPermissionsByRole($role->name, false);
         foreach ($rolePermissions as $permission) {
             if ($permission->name == $item->name) {
                 array_push($roles, $role);
             }
         }
     }
     return $this->renderIsAjax('view', compact('item', 'childPermissions', 'routes', 'permissionsByGroup', 'childRoutes', 'roles'));
 }
Ejemplo 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}]));
     }
 }
Ejemplo n.º 8
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getRoles()
 {
     return $this->hasMany(Role::className(), ['name' => 'item_name'])->viaTable(Yii::$app->getModule('user-management')->auth_assignment_table, ['user_id' => 'id']);
 }
Ejemplo n.º 9
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'));
 }
Ejemplo n.º 10
0
				<div class="col-sm-6 text-right">
					<?php 
echo GridPageSize::widget(['pjaxId' => 'user-grid-pjax']);
?>
				</div>
			</div>


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

			<?php 
echo GridView::widget(['id' => 'user-grid', 'dataProvider' => $dataProvider, 'pager' => ['options' => ['class' => 'pagination pagination-sm'], 'hideOnSinglePage' => true, 'lastPageLabel' => '>>', 'firstPageLabel' => '<<'], 'filterModel' => $searchModel, 'layout' => '{items}<div class="row"><div class="col-sm-8">{pager}</div><div class="col-sm-4 text-right">{summary}' . GridBulkActions::widget(['gridId' => 'user-grid', 'actions' => [Url::to(['bulk-activate', 'attribute' => 'status']) => GridBulkActions::t('app', 'Activate'), Url::to(['bulk-deactivate', 'attribute' => 'status']) => GridBulkActions::t('app', 'Deactivate'), '----' => [Url::to(['bulk-delete']) => GridBulkActions::t('app', 'Delete')]]]) . '</div></div>', 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'options' => ['style' => 'width:10px']], ['class' => 'webvimark\\components\\StatusColumn', 'attribute' => 'superadmin', 'visible' => Yii::$app->user->isSuperadmin], ['attribute' => 'username', 'value' => function (User $model) {
    return Html::a($model->username, ['view', 'id' => $model->id], ['data-pjax' => 0]);
}, 'format' => 'raw'], ['attribute' => 'email', 'format' => 'raw', 'visible' => User::hasPermission('viewUserEmail')], ['class' => 'webvimark\\components\\StatusColumn', 'attribute' => 'email_confirmed', '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')], ['attribute' => 'registration_ip', 'value' => function (User $model) {
    return Html::a($model->registration_ip, "http://ipinfo.io/" . $model->registration_ip, ["target" => "_blank"]);
}, 'format' => 'raw', 'visible' => User::hasPermission('viewRegistrationIp')], ['value' => function (User $model) {
    return GhostHtml::a(UserManagementModule::t('back', 'Roles and permissions'), ['/user-management/user-permission/set', 'id' => $model->id], ['class' => 'btn btn-sm btn-primary', 'data-pjax' => 0]);
}, 'format' => 'raw', 'visible' => User::canRoute('/user-management/user-permission/set'), 'options' => ['width' => '10px']], ['value' => function (User $model) {
    return GhostHtml::a(UserManagementModule::t('back', 'Change password'), ['change-password', 'id' => $model->id], ['class' => 'btn btn-sm btn-default', 'data-pjax' => 0]);
}, 'format' => 'raw', 'options' => ['width' => '10px']], ['class' => 'webvimark\\components\\StatusColumn', 'attribute' => 'status', 'optionsArray' => [[User::STATUS_ACTIVE, UserManagementModule::t('back', 'Active'), 'success'], [User::STATUS_INACTIVE, UserManagementModule::t('back', 'Inactive'), 'warning'], [User::STATUS_BANNED, UserManagementModule::t('back', 'Banned'), 'danger']]], ['class' => 'yii\\grid\\CheckboxColumn', 'options' => ['style' => 'width:10px']], ['class' => 'yii\\grid\\ActionColumn', 'contentOptions' => ['style' => 'width:70px; text-align:center;']]]]);
?>

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

		</div>
Ejemplo n.º 11
0
echo $this->title;
?>
</h2>

	<div class="panel panel-default">
		<div class="panel-body">

		    <p>
			<?php 
echo GhostHtml::a(UserManagementModule::t('back', 'Edit'), ['update', 'id' => $model->id], ['class' => 'btn btn-sm btn-primary']);
?>
			<?php 
echo GhostHtml::a(UserManagementModule::t('back', 'Create'), ['create'], ['class' => 'btn btn-sm btn-success']);
?>
			<?php 
echo GhostHtml::a(UserManagementModule::t('back', 'Roles and permissions'), ['/user-management/user-permission/set', 'id' => $model->id], ['class' => 'btn btn-sm btn-default']);
?>

			<?php 
echo GhostHtml::a(UserManagementModule::t('back', 'Delete'), ['delete', 'id' => $model->id], ['class' => 'btn btn-sm btn-danger pull-right', 'data' => ['confirm' => UserManagementModule::t('back', 'Are you sure you want to delete this user?'), 'method' => 'post']]);
?>
		    </p>

			<?php 
echo DetailView::widget(['model' => $model, 'attributes' => ['id', ['attribute' => 'status', 'value' => User::getStatusValue($model->status)], 'username', ['attribute' => 'email', 'value' => $model->email, 'format' => 'email', 'visible' => User::hasPermission('viewUserEmail')], ['attribute' => 'email_confirmed', 'value' => $model->email_confirmed, 'format' => 'boolean', 'visible' => User::hasPermission('viewUserEmail')], ['label' => UserManagementModule::t('back', 'Roles'), 'value' => implode('<br>', ArrayHelper::map(Role::getUserRoles($model->id), 'name', 'description')), 'visible' => User::hasPermission('viewUserRoles'), 'format' => 'raw'], ['attribute' => 'bind_to_ip', 'visible' => User::hasPermission('bindUserToIp')], array('attribute' => 'registration_ip', 'value' => Html::a($model->registration_ip, "http://ipinfo.io/" . $model->registration_ip, ["target" => "_blank"]), 'format' => 'raw', 'visible' => User::hasPermission('viewRegistrationIp')), 'created_at:datetime', 'updated_at:datetime']]);
?>

		</div>
	</div>
</div>
Ejemplo n.º 12
0
echo UserManagementModule::t('back', 'Roles');
?>
				</strong>
			</div>
			<div class="panel-body">

				<?php 
echo Html::beginForm(['set-roles', 'id' => $user->id]);
?>

				<?php 
foreach (Role::getAvailableRoles() as $aRole) {
    ?>
					<label>
						<?php 
    $isChecked = in_array($aRole['name'], ArrayHelper::map(Role::getUserRoles($user->id), 'name', 'name')) ? 'checked' : '';
    ?>

						<?php 
    if (Yii::$app->getModule('user-management')->userCanHaveMultipleRoles) {
        ?>
							<input type="checkbox" <?php 
        echo $isChecked;
        ?>
 name="roles[]" value="<?php 
        echo $aRole['name'];
        ?>
">

						<?php 
    } else {
Ejemplo n.º 13
0
 public function allowUser()
 {
     $user = Yii::$app->user->getId();
     //print_r($user);exit;
     $role = Role::getUserRoles($user);
     // print_r($role);exit;
     if (empty($role)) {
         return false;
     } else {
         return true;
     }
 }
 public function safeDown()
 {
     Role::deleteAll(['name' => ['ContentManager', 'SeoManager']]);
 }
Ejemplo n.º 15
0
			<div class="panel-heading">
				<strong>
					<span class="glyphicon glyphicon-th"></span> <?php 
echo UserManagementModule::t('back', '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' => UserManagementModule::t('back', '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');
    $isChecked = $checked ? 'checked' : '';
    $checkbox = "<label><input type='checkbox' name='{$name}' value='{$value}' {$isChecked}> {$label}</label>";
    return $helpIcon . ' ' . $checkbox;
}, 'separator' => '<br>']);
?>

				<hr/>
				<?php 
echo Html::submitButton('<span class="glyphicon glyphicon-ok"></span> ' . UserManagementModule::t('back', 'Save'), ['class' => 'btn btn-primary btn-sm']);