Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I need to create a new controller? #20

Open
IlzoJunior opened this issue Jul 12, 2018 · 12 comments
Open

I need to create a new controller? #20

IlzoJunior opened this issue Jul 12, 2018 · 12 comments

Comments

@IlzoJunior
Copy link

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 ?

@yariksav
Copy link

yariksav commented Aug 4, 2018

@IlzoJunior , as you wish, you are developer 😄

@adailsonm
Copy link

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

@erikkallen
Copy link

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

@ajkal5
Copy link

ajkal5 commented May 10, 2020

Eric Kallen,

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

Muchos Gracias

Ajay K

@ajkal5
Copy link

ajkal5 commented May 10, 2020

Eric Kallen,

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

Thanks a bunch.

Ajay K

@ajkal5
Copy link

ajkal5 commented May 11, 2020

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

@erikkallen
Copy link

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

@ajkal5
Copy link

ajkal5 commented May 24, 2020

Hi Erik,

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

Thanks

Ajay K
2020-05-25_003442

@ajkal5
Copy link

ajkal5 commented May 24, 2020

Hi Erik,

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

I have asked same from other issue thread.

Thanks

Ajay K.

@ajkal5
Copy link

ajkal5 commented May 24, 2020

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
Copy link

ajkal5 commented May 24, 2020

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
Copy link

ajkal5 commented May 24, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants