Add user profile form

This commit is contained in:
Ramon Gutierrez 2018-01-31 06:14:46 +08:00
parent d7dc2813e4
commit 6be23b27c5
5 changed files with 162 additions and 62 deletions

View file

@ -19,6 +19,8 @@ access_keys:
label: Delete
- id: user.role.sadmin
label: Super Admin Role
- id: user.profile
label: User Profile
- id: role
label: Role Access
acls:

View file

@ -32,3 +32,12 @@ user_delete:
controller: App\Controller\UserController::destroy
methods: [DELETE]
user_profile:
path: /profile
controller: App\Controller\UserController::profileForm
methods: [GET]
user_profile_submit:
path: /profile
controller: App\Controller\UserController::profileSubmit
methods: [POST]

View file

@ -93,7 +93,6 @@ class UserController extends BaseController
$row['contact_num'] = $orow->getContactNumber();
$row['enabled'] = $orow->isEnabled();
// add row metadata
$row['meta'] = [
'update_url' => '',
@ -145,10 +144,10 @@ class UserController extends BaseController
// create new row
$em = $this->getDoctrine()->getManager();
$row = new User();
$obj = new User();
// set and save values
$row->setUsername($req->request->get('username'))
$obj->setUsername($req->request->get('username'))
->setFirstName($req->request->get('first_name'))
->setLastName($req->request->get('last_name'))
->setEmail($req->request->get('email'))
@ -169,13 +168,13 @@ class UserController extends BaseController
if ($role->isSuperAdmin() && !$this->isGranted('user.role.sadmin'))
continue;
$row->addRole($role);
$obj->addRole($role);
}
}
}
// validate
$errors = $validator->validate($row);
$errors = $validator->validate($obj);
// initialize error list
$error_array = [];
@ -196,11 +195,11 @@ class UserController extends BaseController
$error_array['confirm_password'] = 'Passwords do not match.';
} else {
// encode password
$enc = $ef->getEncoder($row);
$encoded_password = $enc->encodePassword($req->request->get('password'), $row->getSalt());
$enc = $ef->getEncoder($obj);
$encoded_password = $enc->encodePassword($req->request->get('password'), $obj->getSalt());
// set password
$row->setPassword($encoded_password);
$obj->setPassword($encoded_password);
}
// check if any errors were found
@ -212,7 +211,7 @@ class UserController extends BaseController
], 422);
} else {
// validated! save the entity
$em->persist($row);
$em->persist($obj);
$em->flush();
// return successful response
@ -231,17 +230,16 @@ class UserController extends BaseController
// get row data
$em = $this->getDoctrine()->getManager();
$row = $em->getRepository(User::class)->find($id);
$obj = $em->getRepository(User::class)->find($id);
// make sure this row exists
if (empty($row))
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
// get roles
$em = $this->getDoctrine()->getManager();
$params['roles'] = $em->getRepository(Role::class)->findAll();
$params['obj'] = $row;
$params['obj'] = $obj;
// response
return $this->render('user/form.html.twig', $params);
@ -253,14 +251,14 @@ class UserController extends BaseController
// get row data
$em = $this->getDoctrine()->getManager();
$row = $em->getRepository(User::class)->find($id);
$obj = $em->getRepository(User::class)->find($id);
// make sure this row exists
if (empty($row))
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
// set and save values
$row->setUsername($req->request->get('username'))
$obj->setUsername($req->request->get('username'))
->setFirstName($req->request->get('first_name'))
->setLastName($req->request->get('last_name'))
->setEmail($req->request->get('email'))
@ -277,12 +275,12 @@ class UserController extends BaseController
$role = $em->getRepository(Role::class)->find($role_id);
if (!empty($role))
$row->addRole($role);
$obj->addRole($role);
}
}
// validate
$errors = $validator->validate($row);
$errors = $validator->validate($obj);
// initialize error list
$error_array = [];
@ -302,11 +300,11 @@ class UserController extends BaseController
$error_array['confirm_password'] = 'Passwords do not match.';
} else {
// encode password
$enc = $ef->getEncoder($row);
$encoded_password = $enc->encodePassword($req->request->get('password'), $row->getSalt());
$enc = $ef->getEncoder($obj);
$encoded_password = $enc->encodePassword($req->request->get('password'), $obj->getSalt());
// set password
$row->setPassword($encoded_password);
$obj->setPassword($encoded_password);
}
}
@ -336,13 +334,13 @@ class UserController extends BaseController
// get row data
$em = $this->getDoctrine()->getManager();
$row = $em->getRepository(User::class)->find($id);
$obj = $em->getRepository(User::class)->find($id);
if (empty($row))
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
// delete this row
$em->remove($row);
$em->remove($obj);
$em->flush();
// response
@ -362,4 +360,90 @@ class UserController extends BaseController
->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%');
}
}
public function profileForm()
{
$this->denyAccessUnlessGranted('user.profile', null, 'No access.');
$params = $this->initParameters('user_profile');
$params['mode'] = 'profile';
// get row data
$em = $this->getDoctrine()->getManager();
$obj = $this->getUser();
// make sure this row exists
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
$params['obj'] = $obj;
// response
return $this->render('user/form.html.twig', $params);
}
public function profileSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator)
{
$this->denyAccessUnlessGranted('user.profile', null, 'No access.');
// get row data
$em = $this->getDoctrine()->getManager();
$obj = $this->getUser();
// make sure this row exists
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
// set and save values
$obj->setFirstName($req->request->get('first_name'))
->setLastName($req->request->get('last_name'))
->setEmail($req->request->get('email'))
->setContactNumber($req->request->get('contact_no'));
// validate
$errors = $validator->validate($obj);
// initialize error list
$error_array = [];
// add errors to list
foreach ($errors as $error) {
$error_array[$error->getPropertyPath()] = $error->getMessage();
}
// get password inputs
$password = $req->request->get('password');
$confirm_password = $req->request->get('confirm_password');
// custom validation for password fields
if ($password || $confirm_password) {
if ($password != $confirm_password) {
$error_array['confirm_password'] = 'Passwords do not match.';
} else {
// encode password
$enc = $ef->getEncoder($obj);
$encoded_password = $enc->encodePassword($req->request->get('password'), $obj->getSalt());
// set password
$obj->setPassword($encoded_password);
}
}
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
} else {
// validated! save the entity
$em->flush();
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
}
}

View file

@ -599,7 +599,7 @@
</span>
</li>
<li class="m-nav__item">
<a href="header/profile.html" class="m-nav__link">
<a href="{{ url('user_profile') }}" class="m-nav__link">
<i class="m-nav__link-icon flaticon-profile-1"></i>
<span class="m-nav__link-title">
<span class="m-nav__link-wrap">

View file

@ -25,6 +25,9 @@
{% if mode == 'update' %}
Edit User
<small>{{ obj.getUsername() }}</small>
{% elseif mode == 'profile' %}
My Profile
<small>{{ obj.getUsername() }}</small>
{% else %}
New User
{% endif %}
@ -32,7 +35,7 @@
</div>
</div>
</div>
<form id="row-form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ mode == 'update' ? url('user_update_submit', {'id': obj.getId()}) : url('user_create_submit') }}">
<form id="row-form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ mode == 'profile' ? url('user_profile_submit') : (mode == 'update' ? url('user_update_submit', {'id': obj.getId()}) : url('user_create_submit')) }}">
<div class="m-portlet__body">
<div class="form-group m-form__group row no-border">
@ -40,7 +43,7 @@
Username:
</label>
<div class="col-lg-4">
<input type="text" name="username" class="form-control m-input" value="{{ obj.getUsername() }}">
<input type="text" name="username" class="form-control m-input" value="{{ obj.getUsername() }}"{{ mode == 'profile' ? ' disabled' }}>
<div class="form-control-feedback hide" data-field="username"></div>
<span class="m-form__help">Unique alias for this user</span>
</div>
@ -52,7 +55,7 @@
<div class="col-lg-4">
<input type="password" name="password" class="form-control m-input">
<div class="form-control-feedback hide" data-field="password"></div>
{% if row is defined %}
{% if obj is defined %}
<span class="m-form__help">Leave both fields blank for unchanged</span>
{% endif %}
</div>
@ -96,48 +99,50 @@
<div class="form-control-feedback hide" data-field="contact_no"></div>
</div>
</div>
<div class="form-group m-form__group row">
<label class="col-lg-2 col-form-label" data-field="roles">
Roles:
</label>
<div class="col-lg-10">
<div class="m-checkbox-list">
{% for role in roles %}
{% if role.isSuperAdmin and not is_granted('user.role.sadmin') %}
{% else %}
<label class="m-checkbox">
<input type="checkbox" name="roles[]" value="{{ role.getID() }}"{{ role.getID() in obj.getRoles() ? ' checked' : '' }}>
{{ role.getName() }}
<span></span>
</label>
{% endif %}
{% endfor %}
{% if mode != 'profile' %}
<div class="form-group m-form__group row">
<label class="col-lg-2 col-form-label" data-field="roles">
Roles:
</label>
<div class="col-lg-10">
<div class="m-checkbox-list">
{% for role in roles %}
{% if role.isSuperAdmin and not is_granted('user.role.sadmin') %}
{% else %}
<label class="m-checkbox">
<input type="checkbox" name="roles[]" value="{{ role.getID() }}"{{ role.getID() in obj.getRoles() ? ' checked' : '' }}>
{{ role.getName() }}
<span></span>
</label>
{% endif %}
{% endfor %}
</div>
<div class="form-control-feedback hide" data-field="roles"></div>
<span class="m-form__help">Check all roles that apply</span>
</div>
<div class="form-control-feedback hide" data-field="roles"></div>
<span class="m-form__help">Check all roles that apply</span>
</div>
</div>
<div class="form-group m-form__group row">
<label class="col-lg-2 col-form-label" data-field="enabled">
Enabled:
</label>
<div class="col-lg-10">
<span class="m-switch m-switch--icon">
<label>
<input type="checkbox" name="enabled" value="1"{{ obj.isEnabled() ? ' checked' }}>
<span></span>
</label>
</span>
<div class="form-control-feedback hide" data-field="enabled"></div>
<div class="form-group m-form__group row">
<label class="col-lg-2 col-form-label" data-field="enabled">
Enabled:
</label>
<div class="col-lg-10">
<span class="m-switch m-switch--icon">
<label>
<input type="checkbox" name="enabled" value="1"{{ obj.isEnabled() ? ' checked' }}>
<span></span>
</label>
</span>
<div class="form-control-feedback hide" data-field="enabled"></div>
</div>
</div>
</div>
{% endif %}
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions m-form__actions--solid m-form__actions--right">
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-success">Submit</button>
<a href="{{ url('user_list') }}" class="btn btn-secondary">Cancel</a>
<a href="{{ url(mode == 'profile' ? 'home' : 'user_list') }}" class="btn btn-secondary">Cancel</a>
</div>
</div>
</div>
@ -169,7 +174,7 @@
text: 'Your changes have been saved!',
type: 'success',
onClose: function() {
window.location.href = "{{ url('user_list') }}";
window.location.href = "{{ url(mode == 'profile' ? 'home' : 'user_list') }}";
}
});
}).fail(function(response) {