Adding Delete user button and basic functionality

This commit is contained in:
Aran Zaiger 2020-05-23 22:02:04 +03:00
parent 4d934fbcdf
commit 3bfa60db28
11 changed files with 336 additions and 295 deletions

View file

@ -6,7 +6,7 @@ const IceServer = use('App/Models/IceServer');
const EmailUtils = use('App/Utils/EmailUtils'); const EmailUtils = use('App/Utils/EmailUtils');
class AdminApiController { class AdminApiController {
async getUsers({response}) { async getUsers({ response }) {
const users = await User.all(); const users = await User.all();
// console.log(typeof users); // console.log(typeof users);
// return users.rows.map(u => { // return users.rows.map(u => {
@ -14,24 +14,39 @@ class AdminApiController {
// }); // });
return users; return users;
} }
async addStunServer({request, response}) {}
async addTurnServer({request, response}) {}
async testEmailSettings({auth, response}) { async deleteUser({ request, response }) {
console.log('in delete user')
const { id } = request.params
const user = await User.find(id)
let userLinks = await user.links().fetch();
const links = userLinks.rows
const promises = [...links.map(l => (l.delete())), user.delete()];
return await Promise.all(promises);
}
async addStunServer({ request, response }) {}
async addTurnServer({ request, response }) {}
async testEmailSettings({ auth, response }) {
try { try {
if (EmailUtils.sendTestEmail(auth.user)) { if (EmailUtils.sendTestEmail(auth.user)) {
return { return {
code: 0, data: {} code: 0,
data: {}
} }
} }
return { return {
code: 500, message: 'Something went wrong' code: 500,
message: 'Something went wrong'
} }
} catch (e) { } catch (e) {
response.code(500); response.code(500);
return { return {
code: 500, message: e.message code: 500,
message: e.message
} }
} }
} }

View file

@ -15,20 +15,20 @@ class User extends Model {
* A hook to hash the user password before saving * A hook to hash the user password before saving
* it to the database. * it to the database.
*/ */
this.addHook('beforeSave', async (userInstance) => { this.addHook('beforeSave', async(userInstance) => {
if (userInstance.dirty.password) { if (userInstance.dirty.password) {
userInstance.password = await Hash.make(userInstance.password) userInstance.password = await Hash.make(userInstance.password)
} }
}) })
} }
publicJSON() { // publicJSON() {
const u = this.toJSON(); // const u = this.toJSON();
return { // return {
avatar: `https://api.adorable.io/avatars/285/${u.email}.png`, id: u.id, // avatar: `https://api.adorable.io/avatars/285/${u.email}.png`, id: u.id,
name: u.name, isAdmin: u.is_admin // name: u.name, isAdmin: u.is_admin
} // }
} // }
static get hidden() { static get hidden() {
return ['password'] return ['password']

View file

@ -79,7 +79,7 @@ module.exports = {
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection
| |
*/ */
xss: {enabled: true, enableOnOldIE: false}, xss: { enabled: true, enableOnOldIE: false },
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -134,7 +134,7 @@ module.exports = {
csrf: { csrf: {
enable: true, enable: true,
methods: ['POST', 'PUT', 'DELETE'], methods: ['POST', 'PUT', 'DELETE'],
filterUris: [/api\/v1\/client\/\w+/], // All Client API routes filterUris: [/api\/v1\/client\/\w+/, /api\/v1\/admin\/\w+/], // All Client API routes
cookieOptions: {httpOnly: false, sameSite: true, path: '/', maxAge: 7200} cookieOptions: { httpOnly: false, sameSite: true, path: '/', maxAge: 7200 }
} }
} }

View file

@ -12,6 +12,7 @@
"migrate": "npx adonis migration:run -f", "migrate": "npx adonis migration:run -f",
"build": "npm run migrate && npm run build:css && npm run build:applications", "build": "npm run migrate && npm run build:css && npm run build:applications",
"start": "npm run migrate && node server.js", "start": "npm run migrate && node server.js",
"start:dev": "npx adonis serve --dev",
"clean": "bash clean-hot-update.sh", "clean": "bash clean-hot-update.sh",
"test": "node ace test" "test": "node ace test"
}, },

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -3,6 +3,9 @@
<Modal title="CreateUser" :isActive="showCreateUser" @close="showCreateUser=false" acceptText="Create" @accept="createUser()"> <Modal title="CreateUser" :isActive="showCreateUser" @close="showCreateUser=false" acceptText="Create" @accept="createUser()">
test test
</Modal> </Modal>
<Modal title="DeleteUser" :isActive="showDeleteUser" @close="showDeleteUser=false; currentUser=null" acceptText="Delete" rejectText="Cancel" @accept="deleteUser(currentUser)">
Are you sure you want to delete {{user.name}}?
</Modal>
<nav class="level"> <nav class="level">
<div class="level-left"> <div class="level-left">
<div class="level-item"> <div class="level-item">
@ -35,6 +38,7 @@
<th>name</th> <th>name</th>
<th>email</th> <th>email</th>
<th>admin</th> <th>admin</th>
<th>edit</th>
</tr> </tr>
</thead> </thead>
<tr v-for="user in users" :key="user.id"> <tr v-for="user in users" :key="user.id">
@ -49,13 +53,15 @@
<td> <td>
<input class="checkbox" type="checkbox" :checked="user.is_admin" /> <input class="checkbox" type="checkbox" :checked="user.is_admin" />
</td> </td>
<td>
<button v-if="!user.is_admin" class="button" @click="onDeleteClicked(user)">Delete</button>
</td>
</tr> </tr>
</table> </table>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import ChildAvatar, { IChildAvatar } from "../components/child_avatar.vue";
import Services from "../../services/index"; import Services from "../../services/index";
import { mapGetters, mapActions } from "vuex"; import { mapGetters, mapActions } from "vuex";
import Modal from "../../shared/components/Modal/Modal.vue"; import Modal from "../../shared/components/Modal/Modal.vue";
@ -63,22 +69,28 @@ import Modal from "../../shared/components/Modal/Modal.vue";
export default { export default {
name: "Home", name: "Home",
components: { components: {
ChildAvatar,
Modal Modal
}, },
methods: { methods: {
createUser(){ createUser(){
alert('created'); alert('created');
}, },
async deleteUser(user){
console.log(user)
await Services.ApiService.deleteUser(user);
this.showDeleteUser=false;
await this.getUsers()
},
onDeleteClicked(user){
this.showDeleteUser = true;
this.currentUser = user;
},
...mapActions(["getUsers"]) ...mapActions(["getUsers"])
}, },
async created() { async created() {
this.loading = true; this.loading = true;
if (this.users === null) await this.getUsers(); if (this.users === null) await this.getUsers();
this.loading = false; this.loading = false;
// this.connections = await Services.ApiService.getConnections();
// this.users = await Services.ApiService.getAllUsers();
// console.dir(connections);
}, },
computed: { computed: {
// async users() { // async users() {
@ -89,6 +101,8 @@ export default {
return { return {
loading: true, loading: true,
showCreateUser: false, showCreateUser: false,
showDeleteUser: false,
currentUser: null,
}; };
} }
}; };

View file

@ -1,5 +1,15 @@
export default class ApiService { export default class ApiService {
static async deleteUser(user: any) {
try{
return (await fetch(`/api/v1/admin/user/${user.id}`, {method: 'DELETE'})).json();
}
catch (e) {
console.error(`deleteUser ERROR: ${e.message}`);
return e;
}
}
static async getUser(userId?: number) { static async getUser(userId?: number) {
try { try {
return (await fetch('/api/v1/client/user/')).json(); return (await fetch('/api/v1/client/user/')).json();

View file

@ -65,9 +65,9 @@ Route
*/ */
Route.get('/u/books/:bookId/thumbnail', 'BookApiController.getThumbnail') Route.get('/u/books/:bookId/thumbnail', 'BookApiController.getThumbnail')
.middleware(['auth', 'BookContext']) .middleware(['auth', 'BookContext'])
/* /*
/ Pubic CDN Images / Pubic CDN Images
*/ */
Route.get('/u/images/:fileName', 'CdnController.publicImages'); Route.get('/u/images/:fileName', 'CdnController.publicImages');
/* /*
@ -77,6 +77,7 @@ Route.get('/u/images/:fileName', 'CdnController.publicImages');
Route Route
.group(() => { .group(() => {
Route.get('users', 'AdminApiController.getUsers'); Route.get('users', 'AdminApiController.getUsers');
Route.delete('user/:id', 'AdminApiController.deleteUser');
Route.get( Route.get(
'settings/email/test/result', 'AdminApiController.testEmailSettings'); 'settings/email/test/result', 'AdminApiController.testEmailSettings');
}) })