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

Get All Users with given permission #637

Closed
esunea opened this issue Feb 14, 2020 · 2 comments
Closed

Get All Users with given permission #637

esunea opened this issue Feb 14, 2020 · 2 comments

Comments

@esunea
Copy link

esunea commented Feb 14, 2020

Hi !
I need to get all the users with a given permission

I've tried this

    const results = await  getRepository(Permission).find({
      where: {codeName:"permission"},
      relations:["groups"]
    })

Unfortunately the permission entity doesn't include a Group[] column

export declare class Permission {
    id: number;
    name: string;
    codeName: string;
}

so i got this error :

Relation "groups" was not found, please check if it is correct and really exist in your entity.

Would it be possible to add the Many-to-Many relation for both users and groups ?

I fear of a circular reference and i am not sure how to do that, but if it is possible, i could probably do it and make a pull request.

Best regards !

@LoicPoullain LoicPoullain added this to Backlog in Issue tracking via automation Feb 28, 2020
@LoicPoullain
Copy link
Member

Hi @esunea

In which case do you need to get all the users with a given permission?

Here is a way to fetch them:

const codeName = 'perm2';
  const users = await getRepository(User)
    .createQueryBuilder('user')
    .innerJoin('user.userPermissions', 'userPermission')
    .innerJoin('user.groups', 'group')
    .innerJoin('group.permissions', 'groupPermission')
    .where('userPermission.codeName = :codeName', { codeName })
    .orWhere('groupPermission.codeName = :codeName', { codeName })
    .getMany();

@LoicPoullain
Copy link
Member

I'm sorry, the above code does not work on some particular cases. Version 1.8 will include a proper and tested static method for this: #682

@LoicPoullain LoicPoullain mentioned this issue Apr 26, 2020
7 tasks
Issue tracking automation moved this from Work In Progress to Done / Closed This Release Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Issue tracking
  
Done / Closed This Release
Development

No branches or pull requests

2 participants