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 label: Delete
- id: user.role.sadmin - id: user.role.sadmin
label: Super Admin Role label: Super Admin Role
- id: user.profile
label: User Profile
- id: role - id: role
label: Role Access label: Role Access
acls: acls:

View file

@ -32,3 +32,12 @@ user_delete:
controller: App\Controller\UserController::destroy controller: App\Controller\UserController::destroy
methods: [DELETE] 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['contact_num'] = $orow->getContactNumber();
$row['enabled'] = $orow->isEnabled(); $row['enabled'] = $orow->isEnabled();
// add row metadata // add row metadata
$row['meta'] = [ $row['meta'] = [
'update_url' => '', 'update_url' => '',
@ -145,10 +144,10 @@ class UserController extends BaseController
// create new row // create new row
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$row = new User(); $obj = new User();
// set and save values // set and save values
$row->setUsername($req->request->get('username')) $obj->setUsername($req->request->get('username'))
->setFirstName($req->request->get('first_name')) ->setFirstName($req->request->get('first_name'))
->setLastName($req->request->get('last_name')) ->setLastName($req->request->get('last_name'))
->setEmail($req->request->get('email')) ->setEmail($req->request->get('email'))
@ -169,13 +168,13 @@ class UserController extends BaseController
if ($role->isSuperAdmin() && !$this->isGranted('user.role.sadmin')) if ($role->isSuperAdmin() && !$this->isGranted('user.role.sadmin'))
continue; continue;
$row->addRole($role); $obj->addRole($role);
} }
} }
} }
// validate // validate
$errors = $validator->validate($row); $errors = $validator->validate($obj);
// initialize error list // initialize error list
$error_array = []; $error_array = [];
@ -196,11 +195,11 @@ class UserController extends BaseController
$error_array['confirm_password'] = 'Passwords do not match.'; $error_array['confirm_password'] = 'Passwords do not match.';
} else { } else {
// encode password // encode password
$enc = $ef->getEncoder($row); $enc = $ef->getEncoder($obj);
$encoded_password = $enc->encodePassword($req->request->get('password'), $row->getSalt()); $encoded_password = $enc->encodePassword($req->request->get('password'), $obj->getSalt());
// set password // set password
$row->setPassword($encoded_password); $obj->setPassword($encoded_password);
} }
// check if any errors were found // check if any errors were found
@ -212,7 +211,7 @@ class UserController extends BaseController
], 422); ], 422);
} else { } else {
// validated! save the entity // validated! save the entity
$em->persist($row); $em->persist($obj);
$em->flush(); $em->flush();
// return successful response // return successful response
@ -231,17 +230,16 @@ class UserController extends BaseController
// get row data // get row data
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$row = $em->getRepository(User::class)->find($id); $obj = $em->getRepository(User::class)->find($id);
// make sure this row exists // make sure this row exists
if (empty($row)) if (empty($obj))
throw $this->createNotFoundException('The item does not exist'); throw $this->createNotFoundException('The item does not exist');
// get roles // get roles
$em = $this->getDoctrine()->getManager();
$params['roles'] = $em->getRepository(Role::class)->findAll(); $params['roles'] = $em->getRepository(Role::class)->findAll();
$params['obj'] = $row; $params['obj'] = $obj;
// response // response
return $this->render('user/form.html.twig', $params); return $this->render('user/form.html.twig', $params);
@ -253,14 +251,14 @@ class UserController extends BaseController
// get row data // get row data
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$row = $em->getRepository(User::class)->find($id); $obj = $em->getRepository(User::class)->find($id);
// make sure this row exists // make sure this row exists
if (empty($row)) if (empty($obj))
throw $this->createNotFoundException('The item does not exist'); throw $this->createNotFoundException('The item does not exist');
// set and save values // set and save values
$row->setUsername($req->request->get('username')) $obj->setUsername($req->request->get('username'))
->setFirstName($req->request->get('first_name')) ->setFirstName($req->request->get('first_name'))
->setLastName($req->request->get('last_name')) ->setLastName($req->request->get('last_name'))
->setEmail($req->request->get('email')) ->setEmail($req->request->get('email'))
@ -277,12 +275,12 @@ class UserController extends BaseController
$role = $em->getRepository(Role::class)->find($role_id); $role = $em->getRepository(Role::class)->find($role_id);
if (!empty($role)) if (!empty($role))
$row->addRole($role); $obj->addRole($role);
} }
} }
// validate // validate
$errors = $validator->validate($row); $errors = $validator->validate($obj);
// initialize error list // initialize error list
$error_array = []; $error_array = [];
@ -302,11 +300,11 @@ class UserController extends BaseController
$error_array['confirm_password'] = 'Passwords do not match.'; $error_array['confirm_password'] = 'Passwords do not match.';
} else { } else {
// encode password // encode password
$enc = $ef->getEncoder($row); $enc = $ef->getEncoder($obj);
$encoded_password = $enc->encodePassword($req->request->get('password'), $row->getSalt()); $encoded_password = $enc->encodePassword($req->request->get('password'), $obj->getSalt());
// set password // set password
$row->setPassword($encoded_password); $obj->setPassword($encoded_password);
} }
} }
@ -336,13 +334,13 @@ class UserController extends BaseController
// get row data // get row data
$em = $this->getDoctrine()->getManager(); $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'); throw $this->createNotFoundException('The item does not exist');
// delete this row // delete this row
$em->remove($row); $em->remove($obj);
$em->flush(); $em->flush();
// response // response
@ -362,4 +360,90 @@ class UserController extends BaseController
->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); ->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> </span>
</li> </li>
<li class="m-nav__item"> <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> <i class="m-nav__link-icon flaticon-profile-1"></i>
<span class="m-nav__link-title"> <span class="m-nav__link-title">
<span class="m-nav__link-wrap"> <span class="m-nav__link-wrap">

View file

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