Skip to content

Commit

Permalink
feature: user edit form (#163)
Browse files Browse the repository at this point in the history
* feature: user edit form

* fix: user edit form fix

* refactor: test var_dump in result array save

* fix: now the select of schools correctly shows the schools that the user belongs to
  • Loading branch information
ByteCarlos authored Feb 16, 2023
1 parent e14a482 commit 6606b8c
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 6 deletions.
70 changes: 67 additions & 3 deletions app/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function accessRules() {
], [
'allow', // allow authenticated user to perform 'create' and 'update' actions
'actions' => [
'import', 'export', 'clearDB', 'acl', 'backup', 'data', 'exportStudentIdentify', 'syncExport',
'import', 'export', 'update', 'manageUsers', 'clearDB', 'acl', 'backup', 'data', 'exportStudentIdentify', 'syncExport',
'syncImport', 'exportToMaster', 'clearMaster', 'importFromMaster'
], 'users' => ['@'],
],
Expand Down Expand Up @@ -446,6 +446,70 @@ public function actionExportMaster(){
unlink($fileName);
}
}
}

?>
public function actionManageUsers() {
$filter = new Users('search');
$filter->unsetAttributes();
if (isset($_GET['Users'])) {
$filter->attributes = $_GET['Users'];
}
$criteria = new CDbCriteria;
$criteria->condition = "username != 'admin'";
$dataProvider = new CActiveDataProvider('Users', array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 12,
)
));
$this->render('manageUsers', array(
'dataProvider' => $dataProvider,
'filter' => $filter,
));
}

public function actionUpdate($id) {
$model = Users::model()->findByPk($id);
$actual_role = $model->getRole();
$userSchools = UsersSchool::model()->findAllByAttributes(array('user_fk' => $id));
if (isset($_POST['Users'], $_POST['Confirm'])) {
$model->attributes = $_POST['Users'];
if ($model->validate()) {
$password = md5($_POST['Users']['password']);
$confirm = md5($_POST['Confirm']);
if ($password == $confirm) {
$model->password = $password;
if ($model->save()) {
$save = TRUE;
foreach ($_POST['schools'] as $school) {
$userSchool = UsersSchool::model()->findByAttributes(array('school_fk' => $school, 'user_fk' => $model->id));
if( $userSchool == null) {
$userSchool = new UsersSchool;
$userSchool->user_fk = $model->id;
$userSchool->school_fk = $school;
$save = $save && $userSchool->validate() && $userSchool->save();
}
}
if ($save) {
$auth = Yii::app()->authManager;
$auth->revoke($_POST['Role'], $model->id);
$auth->assign($_POST['Role'], $model->id);
Yii::app()->user->setFlash('success', Yii::t('default', 'Usuário alterado com sucesso!'));
$this->redirect(['index']);
}
}
} else {
$model->addError('password', Yii::t('default', 'Confirm Password') . ': ' . Yii::t('help', 'Confirm'));
}
}
}

$result = [];
$i = 0;
foreach ($userSchools as $scholl){
$result[$i] = $scholl->school_fk;
$i++;
}

$this->render('_form', ['model' => $model, 'actual_role' => $actual_role, 'userSchools' => $result]);
}
}
2 changes: 2 additions & 0 deletions app/messages/pt_br/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@
'Password' => 'Senha',
'Confirm Password' => 'Confirmar Senha',
'Username' => 'Usuário',
'Active' => 'Ativo',
'Month' => 'Mês',

//Abas
Expand Down Expand Up @@ -788,6 +789,7 @@
'Update StudentIdentification' => 'Atualizar Aluno',
'Update SchoolIdentification' => 'Atualizar Escola',
'Update InstructorIdentification' => 'Atualizar Professor',
'Update User' => 'Atualizar Usuário',

//Erros
'already enrolled in this classroom.' =>'já matriculado na turma.',
Expand Down
19 changes: 18 additions & 1 deletion app/models/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function search()
// @todo Please modify the following code to remove attributes that should not be searched.

$criteria=new CDbCriteria;

$criteria->condition = "username != 'admin'";
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('username',$this->username,true);
Expand All @@ -113,4 +113,21 @@ public static function model($className=__CLASS__)
{
return parent::model($className);
}

public function getRole()
{

$role = Yii::app()->db->createCommand()

->select('itemname')

->from('auth_assignment')

->where('userid=:id', array(':id'=>$this->id))

->queryScalar();


return $role;
}
}
1 change: 1 addition & 0 deletions js/admin/form/_initialization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('#Users_password').attr('value', '');
11 changes: 9 additions & 2 deletions themes/default/views/admin/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
$baseUrl = Yii::app()->baseUrl;
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl . '/js/admin/form/validations.js', CClientScript::POS_END);
$cs->registerScriptFile($baseUrl . '/js/admin/form/_initialization.js', CClientScript::POS_END);

$form = $this->beginWidget('CActiveForm', array(
'id' => 'users-createUser-form',
Expand Down Expand Up @@ -100,13 +101,19 @@
foreach ($roles as $key => $value) {
$roles[$key] = Yii::t('default',$value);
}
echo CHtml::dropDownList('Role','', $roles ,array('class'=>'select-search-off')); ?>
echo CHtml::dropDownList('Role',$actual_role, $roles ,array('class'=>'select-search-off')); ?>
</div>
</div>
<div class="control-group">
<?php echo CHtml::label( Yii::t('default','Schools'), 'schools', array('class' => 'control-label')); ?>
<div class="controls">
<?php echo CHtml::dropDownList('schools','',CHtml::listData(SchoolIdentification::model()->findAll('situation=1 order by name'), 'inep_id', 'name'),array('multiple'=>'multiple', 'class'=>'select-search-on')); ?>
<?php echo CHtml::dropDownList('schools',$userSchools,CHtml::listData(SchoolIdentification::model()->findAll('situation=1 order by name'), 'inep_id', 'name'),array('multiple'=>'multiple', 'class'=>'select-search-on')); ?>
</div>
</div>
<div class="control-group">
<?php echo CHtml::label( Yii::t('default','Active'), 'active', array('class' => 'control-label')); ?>
<div class="controls">
<?php echo CHtml::activeCheckbox($model,'active') ?>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions themes/default/views/admin/editPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
$baseUrl = Yii::app()->baseUrl;
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl . '/js/admin/form/validations.js', CClientScript::POS_END);
$cs->registerScriptFile($baseUrl . '/js/admin/form/_initialization.js', CClientScript::POS_END);

$form = $this->beginWidget('CActiveForm', array(
'id' => 'users-createUser-form',
Expand Down
19 changes: 19 additions & 0 deletions themes/default/views/admin/editUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div id="mainPage" class="main">
<?php
$this->setPageTitle('TAG - ' . Yii::t('default', 'Update User'));
$title = Yii::t('default', 'Update User');
$contextDesc = Yii::t('default', 'Available actions that may be taken on User.');
$this->menu = array(
array('label' => Yii::t('default', 'List User'),
'url' => array('index'),
'description' => Yii::t('default', 'This action list all User, you can search, delete and update')),
);
?>
<?php
echo $this->renderPartial('_form', array(
'model' => $model,
'title' => $title,
'actual_role' => $actual_role
));
?>
</div>
7 changes: 7 additions & 0 deletions themes/default/views/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
<div class="clearfix"></div>
</a>
</div>
<div class="span3">
<a href="<?php echo Yii::app()->createUrl('admin/manageUsers') ?>" class="widget-stats">
<span class="glyphicons edit"><i></i></span>
<span class="txt">Gerenciar Usuários</span>
<div class="clearfix"></div>
</a>
</div>
</div>
</div>
</div>
Expand Down
56 changes: 56 additions & 0 deletions themes/default/views/admin/manageUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<div id="mainPage" class="main">
<?php
$this->setPageTitle('TAG - ' . Yii::t('default', 'Users'));

$contextDesc = Yii::t('default', 'Available actions that may be taken on Classroom.');
$this->menu = array(
array('label' => Yii::t('default', 'Users'), 'url' => array('create'), 'description' => Yii::t('default', 'This action create a new Classroom')),
);
?>

<div class="innerLR">
<div class="columnone" style="padding-right: 1em">
<?php if (Yii::app()->user->hasFlash('success')): ?>
<div class="alert alert-success">
<?php echo Yii::app()->user->getFlash('success') ?>
</div>
<br/>
<?php endif ?>
<div class="widget">
<div class="widget-body">
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $filter->search(),
'enablePagination' => true,
'filter' => $filter,
'itemsCssClass' => 'table table-condensed table-striped table-hover table-primary table-vertical-center checkboxs',
'columns' => array(
array(
'name' => 'name',
'type' => 'raw',
'value' => 'CHtml::link($data->name,Yii::app()->createUrl("admin/update",array("id"=>$data->id)))',
'htmlOptions' => array('width' => '400px')
),
array(
'name' => 'username',
'type' => 'raw',
'value' => '$data->username',
),
array(
'name' => 'active',
'type' => 'raw',
'value' => '$data->active ? Sim : Não'
),
array(
'class' => 'CButtonColumn',
'template' => '{update}',
),
),
));
?>
</div>
</div>
</div>
</div>

</div>

0 comments on commit 6606b8c

Please # to comment.