2024-03-14 19:25:27 +00:00
|
|
|
import type { HttpContext } from '@adonisjs/core/http';
|
2024-04-30 09:50:50 +00:00
|
|
|
import User from '#models/user';
|
|
|
|
import Role from '#models/role';
|
2024-05-16 11:47:06 +00:00
|
|
|
import { ModelQueryBuilderContract } from '@adonisjs/lucid/types/model';
|
|
|
|
import { createUserValidator, updateUserValidator } from '#validators/user';
|
2024-02-06 15:39:33 +00:00
|
|
|
// import { schema, rules } from '@ioc:Adonis/Core/Validator';
|
|
|
|
// import Hash from '@ioc:Adonis/Core/Hash';
|
2023-03-03 15:54:28 +00:00
|
|
|
// import { schema, rules } from '@ioc:Adonis/Core/Validator';
|
|
|
|
|
2024-04-23 17:36:45 +00:00
|
|
|
export default class AdminuserController {
|
2024-03-14 19:25:27 +00:00
|
|
|
public async index({ auth, request, inertia }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
const page = request.input('page', 1);
|
|
|
|
// const limit = 10
|
|
|
|
|
|
|
|
let users: ModelQueryBuilderContract<typeof User, User> = User.query();
|
|
|
|
|
|
|
|
if (request.input('search')) {
|
|
|
|
// users = users.whereRaw('name like %?%', [request.input('search')])
|
|
|
|
const searchTerm = request.input('search');
|
|
|
|
users.where('login', 'ilike', `%${searchTerm}%`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.input('sort')) {
|
|
|
|
type SortOrder = 'asc' | 'desc' | undefined;
|
|
|
|
let attribute = request.input('sort');
|
2023-07-17 17:13:30 +00:00
|
|
|
let sortOrder: SortOrder = 'asc';
|
2023-06-22 15:20:04 +00:00
|
|
|
|
|
|
|
// if (strncmp($attribute, '-', 1) === 0) {
|
2023-07-17 17:13:30 +00:00
|
|
|
if (attribute.substr(0, 1) === '-') {
|
|
|
|
sortOrder = 'desc';
|
2023-06-22 15:20:04 +00:00
|
|
|
// attribute = substr(attribute, 1);
|
|
|
|
attribute = attribute.substr(1);
|
|
|
|
}
|
|
|
|
// $users->orderBy($attribute, $sort_order);
|
2023-07-17 17:13:30 +00:00
|
|
|
users.orderBy(attribute, sortOrder);
|
2023-06-22 15:20:04 +00:00
|
|
|
} else {
|
|
|
|
// users.orderBy('created_at', 'desc');
|
|
|
|
users.orderBy('id', 'asc');
|
|
|
|
}
|
|
|
|
|
|
|
|
// const users = await User.query().orderBy('login').paginate(page, limit);
|
|
|
|
|
|
|
|
let usersResult = await users // User.query()
|
|
|
|
// .orderBy('login')
|
|
|
|
// .filter(qs)
|
|
|
|
// .preload('focusInterests')
|
|
|
|
// .preload('role')
|
2024-11-29 14:46:26 +00:00
|
|
|
.paginate(page, 10);
|
2024-05-16 11:47:06 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// var test = request.all();
|
|
|
|
|
|
|
|
return inertia.render('Admin/User/Index', {
|
|
|
|
// testing: 'this is a test',
|
|
|
|
users: usersResult.toJSON(),
|
|
|
|
filters: request.all(),
|
|
|
|
can: {
|
|
|
|
create: await auth.user?.can(['user-create']),
|
|
|
|
edit: await auth.user?.can(['user-edit']),
|
|
|
|
delete: await auth.user?.can(['user-delete']),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async create({ inertia }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
// let rolesPluck = {};
|
|
|
|
// (await Role.query().select('id', 'name')).forEach((user) => {
|
|
|
|
// rolesPluck[user.id] = user.name;
|
|
|
|
// });
|
|
|
|
const roles = await Role.query().select('id', 'name').pluck('name', 'id');
|
|
|
|
|
|
|
|
return inertia.render('Admin/User/Create', {
|
|
|
|
roles: roles,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async store({ request, response, session }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
// node ace make:validator CreateUser
|
|
|
|
try {
|
|
|
|
// Step 2 - Validate request body against the schema
|
2024-05-16 11:47:06 +00:00
|
|
|
// await request.validate(CreateUserValidator);
|
|
|
|
await request.validateUsing(createUserValidator);
|
2023-06-22 15:20:04 +00:00
|
|
|
// console.log({ payload });
|
|
|
|
} catch (error) {
|
|
|
|
// Step 3 - Handle errors
|
|
|
|
// return response.badRequest(error.messages);
|
|
|
|
throw error;
|
|
|
|
}
|
2024-10-31 10:02:36 +00:00
|
|
|
const input = request.only(['login', 'email', 'password', 'first_name', 'last_name']);
|
2023-06-22 15:20:04 +00:00
|
|
|
const user = await User.create(input);
|
|
|
|
if (request.input('roles')) {
|
|
|
|
const roles: Array<number> = request.input('roles');
|
|
|
|
await user.related('roles').attach(roles);
|
|
|
|
}
|
|
|
|
|
|
|
|
session.flash('message', 'User has been created successfully');
|
2024-05-16 11:47:06 +00:00
|
|
|
return response.redirect().toRoute('settings.user.index');
|
2023-06-22 15:20:04 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async show({ request, inertia }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
const id = request.param('id');
|
|
|
|
const user = await User.query().where('id', id).firstOrFail();
|
|
|
|
|
|
|
|
const roles = await Role.query().pluck('name', 'id');
|
|
|
|
// const userHasRoles = user.roles;
|
|
|
|
const userRoles = await user.related('roles').query().orderBy('name').pluck('id');
|
|
|
|
|
|
|
|
return inertia.render('Admin/User/Show', {
|
|
|
|
roles: roles,
|
|
|
|
user: user,
|
|
|
|
userHasRoles: userRoles,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async edit({ request, inertia }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
const id = request.param('id');
|
|
|
|
const user = await User.query().where('id', id).firstOrFail();
|
|
|
|
|
|
|
|
const roles = await Role.query().pluck('name', 'id');
|
|
|
|
// const userHasRoles = user.roles;
|
|
|
|
const userHasRoles = await user.related('roles').query().orderBy('name').pluck('id');
|
|
|
|
// let test = Object.keys(userHasRoles).map((key) => userHasRoles[key]);
|
|
|
|
return inertia.render('Admin/User/Edit', {
|
|
|
|
roles: roles,
|
|
|
|
user: user,
|
|
|
|
userHasRoles: Object.keys(userHasRoles).map((key) => userHasRoles[key]), //convert object to array with role ids
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async update({ request, response, session }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
// node ace make:validator UpdateUser
|
|
|
|
const id = request.param('id');
|
|
|
|
const user = await User.query().where('id', id).firstOrFail();
|
|
|
|
|
|
|
|
// validate update form
|
2024-05-16 11:47:06 +00:00
|
|
|
await request.validateUsing(updateUserValidator, {
|
|
|
|
meta: {
|
2024-06-14 10:38:04 +00:00
|
|
|
objId: user.id,
|
2024-05-16 11:47:06 +00:00
|
|
|
},
|
|
|
|
});
|
2023-06-22 15:20:04 +00:00
|
|
|
|
|
|
|
// password is optional
|
|
|
|
let input;
|
|
|
|
if (request.input('password')) {
|
2024-10-31 10:02:36 +00:00
|
|
|
input = request.only(['login', 'email', 'password', 'first_name', 'last_name']);
|
2023-06-22 15:20:04 +00:00
|
|
|
} else {
|
2024-10-31 10:02:36 +00:00
|
|
|
input = request.only(['login', 'email', 'first_name', 'last_name']);
|
2023-06-22 15:20:04 +00:00
|
|
|
}
|
|
|
|
await user.merge(input).save();
|
|
|
|
// await user.save();
|
|
|
|
|
|
|
|
if (request.input('roles')) {
|
|
|
|
const roles: Array<number> = request.input('roles');
|
|
|
|
await user.related('roles').sync(roles);
|
|
|
|
}
|
|
|
|
|
|
|
|
session.flash('message', 'User has been updated successfully');
|
2023-12-29 14:54:49 +00:00
|
|
|
return response.redirect().toRoute('settings.user.index');
|
2023-06-22 15:20:04 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async destroy({ request, response, session }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
const id = request.param('id');
|
|
|
|
const user = await User.findOrFail(id);
|
|
|
|
await user.delete();
|
|
|
|
|
|
|
|
session.flash('message', `User ${user.login} has been deleted.`);
|
2024-05-16 11:47:06 +00:00
|
|
|
return response.redirect().toRoute('settings.user.index');
|
2023-06-22 15:20:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 10:38:04 +00:00
|
|
|
// private async syncRoles(objId: number, roleIds: Array<number>) {
|
|
|
|
// const user = await User.findOrFail(objId)
|
2023-06-22 15:20:04 +00:00
|
|
|
// // const roles: Role[] = await Role.query().whereIn('id', roleIds);
|
|
|
|
|
|
|
|
// // await user.roles().sync(roles.rows.map(role => role.id))
|
|
|
|
// await user.related("roles").sync(roleIds);
|
|
|
|
|
|
|
|
// return user
|
|
|
|
// }
|
2023-03-03 15:54:28 +00:00
|
|
|
}
|