Adding Delete user button and basic functionality
This commit is contained in:
parent
4d934fbcdf
commit
3bfa60db28
11 changed files with 336 additions and 295 deletions
|
@ -6,7 +6,7 @@ const IceServer = use('App/Models/IceServer');
|
|||
|
||||
const EmailUtils = use('App/Utils/EmailUtils');
|
||||
class AdminApiController {
|
||||
async getUsers({response}) {
|
||||
async getUsers({ response }) {
|
||||
const users = await User.all();
|
||||
// console.log(typeof users);
|
||||
// return users.rows.map(u => {
|
||||
|
@ -14,24 +14,39 @@ class AdminApiController {
|
|||
// });
|
||||
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 {
|
||||
if (EmailUtils.sendTestEmail(auth.user)) {
|
||||
return {
|
||||
code: 0, data: {}
|
||||
code: 0,
|
||||
data: {}
|
||||
}
|
||||
}
|
||||
return {
|
||||
code: 500, message: 'Something went wrong'
|
||||
code: 500,
|
||||
message: 'Something went wrong'
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
response.code(500);
|
||||
return {
|
||||
code: 500, message: e.message
|
||||
code: 500,
|
||||
message: e.message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,20 +15,20 @@ class User extends Model {
|
|||
* A hook to hash the user password before saving
|
||||
* it to the database.
|
||||
*/
|
||||
this.addHook('beforeSave', async (userInstance) => {
|
||||
this.addHook('beforeSave', async(userInstance) => {
|
||||
if (userInstance.dirty.password) {
|
||||
userInstance.password = await Hash.make(userInstance.password)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
publicJSON() {
|
||||
const u = this.toJSON();
|
||||
return {
|
||||
avatar: `https://api.adorable.io/avatars/285/${u.email}.png`, id: u.id,
|
||||
name: u.name, isAdmin: u.is_admin
|
||||
}
|
||||
}
|
||||
// publicJSON() {
|
||||
// const u = this.toJSON();
|
||||
// return {
|
||||
// avatar: `https://api.adorable.io/avatars/285/${u.email}.png`, id: u.id,
|
||||
// name: u.name, isAdmin: u.is_admin
|
||||
// }
|
||||
// }
|
||||
|
||||
static get hidden() {
|
||||
return ['password']
|
||||
|
|
|
@ -79,7 +79,7 @@ module.exports = {
|
|||
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: {
|
||||
enable: true,
|
||||
methods: ['POST', 'PUT', 'DELETE'],
|
||||
filterUris: [/api\/v1\/client\/\w+/], // All Client API routes
|
||||
cookieOptions: {httpOnly: false, sameSite: true, path: '/', maxAge: 7200}
|
||||
filterUris: [/api\/v1\/client\/\w+/, /api\/v1\/admin\/\w+/], // All Client API routes
|
||||
cookieOptions: { httpOnly: false, sameSite: true, path: '/', maxAge: 7200 }
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@
|
|||
"migrate": "npx adonis migration:run -f",
|
||||
"build": "npm run migrate && npm run build:css && npm run build:applications",
|
||||
"start": "npm run migrate && node server.js",
|
||||
"start:dev": "npx adonis serve --dev",
|
||||
"clean": "bash clean-hot-update.sh",
|
||||
"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
|
@ -3,6 +3,9 @@
|
|||
<Modal title="CreateUser" :isActive="showCreateUser" @close="showCreateUser=false" acceptText="Create" @accept="createUser()">
|
||||
test
|
||||
</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">
|
||||
<div class="level-left">
|
||||
<div class="level-item">
|
||||
|
@ -35,6 +38,7 @@
|
|||
<th>name</th>
|
||||
<th>email</th>
|
||||
<th>admin</th>
|
||||
<th>edit</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tr v-for="user in users" :key="user.id">
|
||||
|
@ -49,13 +53,15 @@
|
|||
<td>
|
||||
<input class="checkbox" type="checkbox" :checked="user.is_admin" />
|
||||
</td>
|
||||
<td>
|
||||
<button v-if="!user.is_admin" class="button" @click="onDeleteClicked(user)">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import ChildAvatar, { IChildAvatar } from "../components/child_avatar.vue";
|
||||
import Services from "../../services/index";
|
||||
import { mapGetters, mapActions } from "vuex";
|
||||
import Modal from "../../shared/components/Modal/Modal.vue";
|
||||
|
@ -63,22 +69,28 @@ import Modal from "../../shared/components/Modal/Modal.vue";
|
|||
export default {
|
||||
name: "Home",
|
||||
components: {
|
||||
ChildAvatar,
|
||||
Modal
|
||||
},
|
||||
methods: {
|
||||
createUser(){
|
||||
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"])
|
||||
},
|
||||
async created() {
|
||||
this.loading = true;
|
||||
if (this.users === null) await this.getUsers();
|
||||
this.loading = false;
|
||||
// this.connections = await Services.ApiService.getConnections();
|
||||
// this.users = await Services.ApiService.getAllUsers();
|
||||
// console.dir(connections);
|
||||
},
|
||||
computed: {
|
||||
// async users() {
|
||||
|
@ -89,6 +101,8 @@ export default {
|
|||
return {
|
||||
loading: true,
|
||||
showCreateUser: false,
|
||||
showDeleteUser: false,
|
||||
currentUser: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,5 +1,15 @@
|
|||
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) {
|
||||
try {
|
||||
return (await fetch('/api/v1/client/user/')).json();
|
||||
|
|
|
@ -65,9 +65,9 @@ Route
|
|||
*/
|
||||
Route.get('/u/books/:bookId/thumbnail', 'BookApiController.getThumbnail')
|
||||
.middleware(['auth', 'BookContext'])
|
||||
/*
|
||||
/ Pubic CDN Images
|
||||
*/
|
||||
/*
|
||||
/ Pubic CDN Images
|
||||
*/
|
||||
Route.get('/u/images/:fileName', 'CdnController.publicImages');
|
||||
|
||||
/*
|
||||
|
@ -77,6 +77,7 @@ Route.get('/u/images/:fileName', 'CdnController.publicImages');
|
|||
Route
|
||||
.group(() => {
|
||||
Route.get('users', 'AdminApiController.getUsers');
|
||||
Route.delete('user/:id', 'AdminApiController.deleteUser');
|
||||
Route.get(
|
||||
'settings/email/test/result', 'AdminApiController.testEmailSettings');
|
||||
})
|
||||
|
|
Reference in a new issue