[go: nahoru, domu]

Skip to content

Commit

Permalink
[new] added option to delete user (#331)
Browse files Browse the repository at this point in the history
- can delete users that have no booking, reviews, or issues
  • Loading branch information
dynamic11 authored Nov 8, 2019
1 parent e46415f commit b6e3c95
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 91 deletions.
89 changes: 62 additions & 27 deletions app/Controllers/Http/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,38 +415,73 @@ class UserController {
return response.redirect('/login');
}

async show ({ auth, params, view, response, request }) {
let user = await User
.query()
.where('id', params.id)
.with('floor')
.with('tower')
.with('building')
.with('role')
.firstOrFail();

var canEdit = 0;
const userRole = await auth.user.getUserRole();
user = user.toJSON();
async show ({ auth, params, view, response, request, session }) {
try {
// find user
let user = await User
.query()
.where('id', params.id)
.with('floor')
.with('tower')
.with('building')
.with('role')
.withCount('bookings')
.withCount('reports')
.withCount('reviews')
.firstOrFail();

var canEdit = 0;
const userRole = await auth.user.getUserRole();
user = user.toJSON();

let selectedBuilding, allBuildings;
let selectedBuilding, allBuildings;

if (userRole === 'admin') {
selectedBuilding = request.cookie('selectedBuilding');
// get all builig info admin nav bar since this route is shared with regular users and admin
// therefore, the admin middle-ware can't retrieve building info to pass to view
allBuildings = await Building.all();
allBuildings = allBuildings.toJSON();
}
if (userRole === 'admin') {
selectedBuilding = request.cookie('selectedBuilding');
// get all builig info admin nav bar since this route is shared with regular users and admin
// therefore, the admin middle-ware can't retrieve building info to pass to view
allBuildings = await Building.all();
allBuildings = allBuildings.toJSON();
}

// check if user is viewing their own profile or is admin
if (auth.user.id === Number(params.id) || userRole === 'admin') {
canEdit = 1;
} else {
return response.redirect('/');
// check if user is viewing their own profile or is admin
if (auth.user.id === Number(params.id) || userRole === 'admin') {
canEdit = 1;
} else {
return response.redirect('/');
}

return view.render('auth.showProfile', { auth, user, canEdit, allBuildings, selectedBuilding });
} catch (error) {
return response.route('home');
}
}

async delete ({ auth, params, response }) {
try {
const userRole = await auth.user.getUserRole();

return view.render('auth.showProfile', { auth, user, canEdit, allBuildings, selectedBuilding });
if (userRole === 'admin' || auth.user.id === Number(params.id)) {
// find user
let user = await User
.query()
.where('id', params.id)
.withCount('bookings')
.withCount('reports')
.withCount('reviews')
.firstOrFail();

let userJSON = user.toJSON();

// check if user has bookings, reviews, or reported issues
if (!userJSON.__meta__.bookings_count && !userJSON.__meta__.reports_count && !userJSON.__meta__.reviews_count) {
await user.delete();
}
}
return response.route('home');
} catch (error) {
return response.route('home');
}
}

/**
Expand Down
Empty file added app/Validators/DeleteProfile.js
Empty file.
12 changes: 11 additions & 1 deletion resources/locales/en/userProfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
"building": "Building",
"ft": "Floor/Tower",
"viewBookings": "View bookings",
"bookings": "bookings",
"editProfile": "Edit profile",
"changePassword": "Change password",
"newPassword": "New password",
"confirmNewPassword": "Confirm new password",
"verified": "verified",
"save":" Save"
"verificationNeeded": "verified",
"save":" Save",
"deleteAccount":"Delete Account",
"deleteWarning1":"The deletion of an account is permanent!",
"deleteWarning2":"proceed with caution",
"deleteUnauthorized":"This account cannot be deleted becausue it has bookings or reviews associated with it",
"deleteConfirm1":"Are you sure want to delete",
"deleteConfirm2":"This user will be permanently removed from Jarvis",
"deleteConfirm3":"This is process is permanent!",
"cancelBtn": "Cancel"
}
12 changes: 11 additions & 1 deletion resources/locales/fr/userProfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
"building": "Édifice",
"ft": "Étage/Tour",
"viewBookings": "Afficher les réservations",
"bookings": "réservations",
"editProfile": "Modifier votre profil",
"changePassword": "Changer mot de passe",
"newPassword": "Nouveau mot de passe",
"confirmNewPassword": "Confirmer le nouveau mot de passe",
"verified": "verifié",
"save": "enregistrer"
"verificationNeeded": "vérification nécessaire",
"save": "enregistrer",
"deleteAccount":"Supprimer votre compte",
"deleteWarning1":"Ce compte sera definitivement supprime!",
"deleteWarning2":"Continuer avec prudence",
"deleteUnauthorized":"Ce compte ne peut pas etre supprime car il a des reservations ou commentaires",
"deleteConfirm1":"Voulez-vous vraiment supprimer",
"deleteConfirm2":"Cet utilisateur sera completement enlever de la base de donnee",
"deleteConfirm3":"Ce processus est permanent!",
"cancelBtn": "Annuler"
}
154 changes: 109 additions & 45 deletions resources/views/auth/showProfile.edge
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<div class='row'>
<div class='col-md-2 text-center'>
<i class='fas fa-user-circle fa-6x mb-3'></i>
<h4>{{ user.firstname }} {{ user.lastname }}</h4>
<h2 class="h4">{{ user.firstname }} {{ user.lastname }}</h2>
</div>
<div class= 'col-md-8'>
<p><strong>{{ antl.formatMessage('userProfile.name') }}:</strong> {{ user.firstname }} {{ user.lastname }}</p>
<p><strong>{{ antl.formatMessage('userProfile.email') }}:</strong> {{ user.email }}
@if(user.verified)
<span class='badge badge-pill badge-success'>{{ antl.formatMessage('userProfile.verified') }}</span>
@else
<span class='badge badge-pill badge-warning'>verification needed</span>
<span class='badge badge-pill badge-warning'>{{ antl.formatMessage('userProfile.verificationNeeded') }}</span>
@endif
</p>
@if(user.role.name==='user')
Expand All @@ -27,6 +27,14 @@
@else
<p><strong>{{ antl.formatMessage('userProfile.ft') }}:</strong> {{ user.floor.name_french }} {{ user.tower.name_french}}</p>
@endif
<div class="d-block mb-2">
<span class="badge badge-primary">{{ antl.formatMessage('userProfile.bookings') }}: {{user.__meta__.bookings_count}}</span>
<span class="badge badge-primary">Reviews: {{user.__meta__.reviews_count}}</span>
<span class="badge badge-primary">Issues: {{user.__meta__.reports_count}}</span>
</div>



<button onclick="window.location='{{ route('viewBookings', {bookingType: 'user', id: user.id, catFilter: 'upcoming', limitFilter: 'all'}) }}';" class="btn btn-info btn-icon-split mt-2">
<span class="icon text-white-50"><i class="fas fa-list-ul"></i></span>
<span class="text">{{ antl.formatMessage('userProfile.viewBookings') }}</span>
Expand All @@ -45,53 +53,109 @@
</div>
</div>
</div>
<div class='row'>
<div class= 'col-12'>
<div class="card shadow mb-4 p-3">
<div class="card-body">
<div class='row'>
<div class='col-md-5'>
@if(canEdit)
<h4>{{ antl.formatMessage('userProfile.changePassword') }}</h4>
@if(old('error'))
<div class='alert alert-danger'>
{{ old('error') }}
</div>
@endif
@if(old('success'))
<div class='alert alert-success'>
{{ old('success') }}
</div>
@endif
<form action='{{ route('changePassword') }}' method='POST'>
{{ csrfField() }}
<input type='hidden' name='userId' value='{{ user.id }}' />

<div class='form-group mb-2'>
<label for='newPassword'>{{ antl.formatMessage('userProfile.newPassword') }}</label>
<input type='password' class='form-control' name='newPassword' id='newPassword' />
@if(hasErrorFor('newPassword'))
<div class='invalid-feedback d-block'>{{ getErrorFor('newPassword') }}</div>
@endif
</div>

<div class='form-group mb-2'>
<label for='confirmPassword'>{{ antl.formatMessage('userProfile.confirmNewPassword') }}</label>
<input type='password' class='form-control' name='confirmPassword' id='confirmPassword' />
@if(hasErrorFor('confirmPassword'))
<div class='invalid-feedback d-block'>{{ getErrorFor('confirmPassword') }}</div>
@endif
</div>

<button type='submit' class='btn btn-primary mt-3'>{{ antl.formatMessage('userProfile.save') }}</button>
</form>
@endif
@if(canEdit)
<div class='row'>
<div class= 'col-12'>
<div class="card shadow mb-4 p-3">
<div class="card-body">
<div class='row'>
<div class='col-md-5'>
<h2 class="h4">{{ antl.formatMessage('userProfile.changePassword') }}</h2>
@if(old('error'))
<div class='alert alert-danger'>
{{ old('error') }}
</div>
@endif
@if(old('success'))
<div class='alert alert-success'>
{{ old('success') }}
</div>
@endif
<form action='{{ route('changePassword') }}' method='POST'>
{{ csrfField() }}
<input type='hidden' name='userId' value='{{ user.id }}' />

<div class='form-group mb-2'>
<label for='newPassword'>{{ antl.formatMessage('userProfile.newPassword') }}</label>
<input type='password' class='form-control' name='newPassword' id='newPassword' />
@if(hasErrorFor('newPassword'))
<div class='invalid-feedback d-block'>{{ getErrorFor('newPassword') }}</div>
@endif
</div>

<div class='form-group mb-2'>
<label for='confirmPassword'>{{ antl.formatMessage('userProfile.confirmNewPassword') }}</label>
<input type='password' class='form-control' name='confirmPassword' id='confirmPassword' />
@if(hasErrorFor('confirmPassword'))
<div class='invalid-feedback d-block'>{{ getErrorFor('confirmPassword') }}</div>
@endif
</div>

<button type='submit' class='btn btn-primary mt-3'>{{ antl.formatMessage('userProfile.save') }}</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endif

@if(canEdit)
<div class='row'>
<div class= 'col-12'>
<div class="card shadow mb-4 p-3">
<div class="card-body">
<div class='row'>
<div class='col-md-5'>
<h2 class="h4"><i class="fas fa-exclamation-triangle text-muted"></i> {{ antl.formatMessage('userProfile.deleteAccount') }}</h2>
@if(!user.__meta__.bookings_count && !user.__meta__.reports_count && !user.__meta__.reviews_count)
<p class="mt-4 mb-1"><strong>{{ antl.formatMessage('userProfile.deleteWarning1') }}</strong></p>
<p class="mt-1">{{ antl.formatMessage('userProfile.deleteWarning2') }}</p>
<button class='btn btn-danger btn-icon-split mt-2' name='delete profile' data-toggle='modal' data-target='#deleteProfileModal'>
<span class="icon text-white-50"><i class="fas fa-trash"></i></span>
<span class="text">{{ antl.formatMessage('userProfile.deleteAccount') }}</span>
</button>

@else
<p class="mt-4">{{ antl.formatMessage('userProfile.deleteUnauthorized') }}</p>
@endif
</div>
</div>
</div>
</div>
</div>
</div>
@endif

@if(!user.__meta__.bookings_count && !user.__meta__.reports_count && !user.__meta__.reviews_count)
{{-- delete Feature modal --}}
<div class='modal' id='deleteProfileModal' role='dialog' aria-hidden='true'>
<div class='modal-dialog modal-dialog-centered' role='document'>
<div class='modal-content'>
<div class='modal-header'>
<h5 class='modal-title' id='cancelTitle'>{{ antl.formatMessage('userProfile.deleteAccount') }}?</h5>
<button class='close' type='button' data-dismiss='modal' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
</div>
<div class='modal-body'>{{ antl.formatMessage('userProfile.deleteConfirm1') }} <strong>{{user.firstname}} {{user.lastname}}</strong>?<br>
<p>{{ antl.formatMessage('userProfile.deleteConfirm2') }}</p>
<p class="text-danger">{{ antl.formatMessage('userProfile.deleteConfirm3') }}</p>
</div>
</div>

<div class='modal-footer'>
<form action='{{ route('deleteProfile', {id: user.id}) }}' method='POST' enctype='multipart/form-data'>
{{ csrfField() }}
<button class='btn btn-secondary' type='button' data-dismiss='modal'>{{ antl.formatMessage('userProfile.cancelBtn') }}</button>
<button type='submit' class='btn btn-danger'>{{ antl.formatMessage('userProfile.deleteAccount') }}</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endif
@endsection


Expand Down
Loading

0 comments on commit b6e3c95

Please sign in to comment.