adonis-acl icon indicating copy to clipboard operation
adonis-acl copied to clipboard

I need to create a new controller?

Open IlzoJunior opened this issue 5 years ago • 12 comments

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 avatar Jul 12 '18 14:07 IlzoJunior

@IlzoJunior , as you wish, you are developer 😄

yariksav avatar Aug 04 '18 10:08 yariksav

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

adailsonm avatar Aug 24 '18 19:08 adailsonm

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

erikkallen avatar Sep 05 '18 06:09 erikkallen

Eric Kallen,

This post has been very helpful. This helped move to next steps in Adonis ACL.

Muchos Gracias

Ajay K

ajkal5 avatar May 10 '20 22:05 ajkal5

Eric Kallen,

Can you share a sample User Controller with ACL for a single table Posts.

Thanks a bunch.

Ajay K

ajkal5 avatar May 10 '20 22:05 ajkal5

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

ajkal5 avatar May 11 '20 20:05 ajkal5

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

erikkallen avatar May 20 '20 08:05 erikkallen

Hi Erik,

with same route as above, I am getting Invalid Expression.

Thanks

Ajay K 2020-05-25_003442

ajkal5 avatar May 24 '20 19:05 ajkal5

Hi Erik,

Can I use 'is:(administrator || moderator)' in middleware?

I have asked same from other issue thread.

Thanks

Ajay K.

ajkal5 avatar May 24 '20 19:05 ajkal5

Hi Erik,

Now how to get assigned roles for given user:

  1. after login currently I return the token.
  2. Another Action within Users Controller can return user role(s).
  3. This list need to be consumed on friend end.

Thanks

Ajay K

ajkal5 avatar May 24 '20 20:05 ajkal5

Hi Erik,

I have taken care of this issue, with my trial and error. 2020-05-25_043126 2020-05-25_043250 2020-05-25_043341 2020-05-25_043411

Thanks

Ajay K

ajkal5 avatar May 24 '20 23:05 ajkal5

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:

  1. some app specific directories
  2. list of backend and frontend models.
  3. for each model in list, all column types, constraints and referential key
  4. frontend color theming
  5. frontend slides with logo for application.
  6. 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

ajkal5 avatar May 24 '20 23:05 ajkal5