Ejemplo n.º 1
0
 /**
  * Database checking
  * @return void
  */
 protected function checkDatabase()
 {
     //schema
     $db = Yii::$app->db;
     $filename = dirname(__DIR__) . '/schema/' . $db->driverName . '.sql';
     $sql = explode(';', file_get_contents($filename));
     foreach ($sql as $s) {
         if (trim($s) !== '') {
             $db->createCommand($s)->execute();
         }
     }
     //rbac
     $auth = Yii::$app->getAuthManager();
     if ($auth->getRole('author') === null) {
         //author role
         $author = $auth->createRole('author');
         $auth->add($author);
         //author rule
         $rule = new AuthorRule();
         $auth->add($rule);
         //author permission
         $own = $auth->createPermission('own');
         $own->ruleName = $rule->name;
         $auth->add($own);
         //add permission with rule to role
         $auth->addChild($author, $own);
     }
     //data
     if (User::find()->andWhere(['admin' => true, 'active' => true])->count() == 0) {
         $model = new User(['admin' => true, 'active' => true, 'email' => 'admin']);
         $model->setPassword('admin');
         $model->save();
     }
 }
Ejemplo n.º 2
0
 /**
  * User editing
  * @param integer $id User id
  * @return void
  */
 public function actionUpdate($id)
 {
     $item = User::findOne($id);
     if ($item === null) {
         throw new BadRequestHttpException(Yii::t('user', 'User not found.'));
     }
     $model = new UserForm(['item' => $item]);
     if ($model->load(Yii::$app->request->post()) && $model->update()) {
         Yii::$app->session->setFlash('success', Yii::t('user', 'Changes saved successfully.'));
         return $this->redirect(['index']);
     }
     return $this->render('update', ['model' => $model]);
 }
Ejemplo n.º 3
0
 /**
  * Search function
  * @param array $params Attributes array
  * @return yii\data\ActiveDataProvider
  */
 public function search($params)
 {
     //ActiveQuery
     $query = User::find()->andWhere(['not', ['id' => Yii::$app->getUser()->id]]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     //return data provider if no search
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     //search
     $query->andFilterWhere(['like', 'email', $this->email]);
     return $dataProvider;
 }
Ejemplo n.º 4
0
 public function run($term)
 {
     //query conditions
     $query = User::find()->andFilterWhere(['like', 'email', $term]);
     if (!$this->admin) {
         $query->andWhere(['not', ['admin' => true]]);
     }
     //get items
     $rows = $query->limit($this->limit)->all();
     //make autocomplete array
     $items = array_map(function ($v) {
         $html = Html::encode($v->email);
         $username = $v->username;
         if ($username !== $v->email) {
             $html .= ' ' . Html::tag('span', Html::encode($username), ['class' => 'text-muted']);
         }
         return ['label' => $v->email, 'value' => $v->email, 'id' => (string) $v->id, 'html' => $html];
     }, $rows);
     return Json::encode($items);
 }
Ejemplo n.º 5
0
 /**
  * User getter
  * @return app\modules\user\common\models\User
  */
 public function getUser()
 {
     if ($this->_user === false) {
         $this->_user = User::findByEmail($this->email);
     }
     return $this->_user;
 }
Ejemplo n.º 6
0
<?php

use yii\data\ArrayDataProvider;
use yii\grid\GridView;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\jui\AutoComplete;
use yii\web\JsExpression;
use user\backend\assets\UserAsset;
use user\common\models\User;
//js
UserAsset::register($this);
//users
$dataProvider = new ArrayDataProvider(['allModels' => User::findAll($model->users), 'pagination' => false]);
//attribute name
$name = Html::getInputName($model, 'users') . '[]';
?>
<fieldset>

	<div class="form-group">
		<div class="col-sm-8 col-md-6">
			<div class="input-group">
				<?php 
echo AutoComplete::widget(['name' => 'email', 'options' => ['class' => 'form-control'], 'clientOptions' => ['source' => Url::toRoute('users'), 'create' => new JsExpression('function(event, ui) {
							$("#role-form input.ui-autocomplete-input").autocomplete("instance")._renderItem = function(ul, item) {
								return $("<li>").html(item.html).appendTo(ul);
							};
						}')]]);
?>
				<span class="input-group-btn">
					<?php 
Ejemplo n.º 7
0
 /**
  * User role assignment
  * @param string $email User email
  * @return void
  */
 public function actionAssign($email)
 {
     //user
     $user = User::findByEmail($email);
     if ($user === null) {
         return Json::encode(['error' => Yii::t('user', 'User not found.')]);
     }
     $model = new RoleForm(['users' => [$user->id]]);
     return Json::encode(['content' => $this->renderPartial('form/assignment', ['model' => $model])]);
 }