adonis-acl
adonis-acl copied to clipboard
I need to create a new controller?
To attach the code "const roleAdmin = new Role() roleAdmin.name = 'Administrator' roleAdmin.slug = 'administrator' roleAdmin.description = 'manage administration privileges' await roleAdmin.save()"
i need to do this in a new RoleController or in the UserController ?
@IlzoJunior , as you wish, you are developer 😄
But how do the addition of these admin roles as done in the table? automatically? Why would I add the controller is it would not work directly including loading the table into the database
I did mine in the database seeder https://adonisjs.com/docs/4.1/seeds-and-factories after migrating the database from scratch you run adonis seed
and you're done
This is what my userSeeder.js looks lke
const Company = use('App/Models/Company')
const User = use('App/Models/User')
// const Hash = use('Hash')
const Role = use('Role')
class UserSeeder {
async run () {
// ==========================================================================================================================================
// Create roles
// ==========================================================================================================================================
const roleAdmin = await Role.findOrCreate({
name: 'Administrator'
},
{
name: 'Administrator',
slug: 'administrator',
description: 'manage administration privileges'
}
)
// ==========================================================================================================================================
// Create users
// ===========================================================================================================================================
let user = await User.findOrCreate({
username: "admin"
}, {
username: "admin",
email: "[email protected]",
password: "12345678"
}
)
await user.roles().attach([roleAdmin.id])
}
}
module.exports = UserSeeder
Eric Kallen,
This post has been very helpful. This helped move to next steps in Adonis ACL.
Muchos Gracias
Ajay K
Eric Kallen,
Can you share a sample User Controller with ACL for a single table Posts.
Thanks a bunch.
Ajay K
Hi Eric,
I have seeded permissions also.
const Permission = use('Permission')
class UserSeeder { async run () { // Create Admin role
const createUsersPermission = new Permission()
createUsersPermission.slug = 'create_users'
createUsersPermission.name = 'Create Users'
createUsersPermission.description = 'Create Users Permission'
await createUsersPermission.save()
const updateUsersPermission = new Permission()
updateUsersPermission.slug = 'update_users'
updateUsersPermission.name = 'Update Users'
updateUsersPermission.description = 'Update Users Permission'
await updateUsersPermission.save()
const deleteUsersPermission = new Permission()
deleteUsersPermission.slug = 'delete_users'
deleteUsersPermission.name = 'Delete Users'
deleteUsersPermission.description = 'Delete Users Permission'
await deleteUsersPermission.save()
const readUsersPermission = new Permission()
readUsersPermission.slug = 'read_users'
readUsersPermission.name = 'Read Users'
readUsersPermission.description = 'Read Users Permission'
await readUsersPermission.save()
const roleAdministrator = await Role.findOrCreate({
name: 'Administrator'
},
{
name: 'Administrator',
slug: 'administrator',
description: 'Administrator Privileges'
}
)
roleAdministrator.permissions().attach([
createUsersPermission.id,
updateUsersPermission.id,
deleteUsersPermission.is,
readUsersPermission.id
])
Thanks
Ajay K
Hi Ajay,
I don't fully understand what you need but this is how I query users in my users controller using the auth middleware with jwt.
async index ({ response }) {
const users = await User.query().with('roles').with('company').fetch()
return response.json(users)
}
async show ({ params, response }) {
const user = await User.query().with('roles').where('id', params.id).first()
return response.json(user)
}
async destroy ({ auth, params, response }) {
const me = auth.current.user.id
const user = await User.query().with('roles').where('id', params.id).andWhereNot('id', me).first()
if (!user) {
return response.status(404).json(null)
}
// Logger.info("Reports:", JSON.stringify(report, null, 2))
return user.delete()
/** rendering view */
// return view.render('report.index', {reports: JSON.stringify(reports)})
}
and my routers.js file looks like this
Route.resource('/user', 'UserController')
.apiOnly()
.middleware(['auth:jwt', 'is:administrator'])
hope that helps
Hi Erik,
with same route as above, I am getting Invalid Expression.
Thanks
Ajay K
Hi Erik,
Can I use 'is:(administrator || moderator)' in middleware?
I have asked same from other issue thread.
Thanks
Ajay K.
Hi Erik,
Now how to get assigned roles for given user:
- after login currently I return the token.
- Another Action within Users Controller can return user role(s).
- This list need to be consumed on friend end.
Thanks
Ajay K
Hi Erik,
I have taken care of this issue, with my trial and error.
Thanks
Ajay K
Hi Erik,
I am in the process of automating app development (end to end). Phase 1 was complete on 24th April 2020.
DB (mysql, sqlite, oracle, pgsql) Backend AdonisJS RestFul Frontend Ionic
End to end automation.
Inputs to the tool are:
- some app specific directories
- list of backend and frontend models.
- for each model in list, all column types, constraints and referential key
- frontend color theming
- frontend slides with logo for application.
- frontend icons to use for each of the models.
Phase 2 integrating ACL. Backend is complete.
Today evening onwards starting on Frontend.
Thanks for your input.
Ajay K
Thanks
Ajay K