如何找到相同角色的用户

huangapple go评论70阅读模式
英文:

How to find users in the same role

问题

以下是您要翻译的内容:

有一个用户实体,它有一个角色列,可能是:ADMIN、TENANT、LANDLORD。我使用 Postman 发出请求以输出所有 TENANTS,我如何从用户中选择 TENANT?

当我最初想要遍历所有角色并找到合适的角色时,我这样做对吗?
然后将选定的角色添加到新列表中。
然后遍历此列表,并按用户通过的 ID 输出用户请求的角色(这里需要帮助)。

我有以下选项:

@GetMapping("/{id}")
@PreAuthorize("hasAuthority('user:read')")
public User landlordGet(@PathVariable(value = "id") Long id) {
    List<User> sort = userRepository.findAll();
    List<User> res = new ArrayList<>();
    for (User us : sort) {
        if (us.getRole().equals(Role.TENANT)) {
            res.add(us);
        }
    }
    ...

    return user;
}
英文:

There is a USER entity, it has a ROLE column, it can be: ADMIN,TENANT,LANDLORD. I use Postman to make a request to output all TENANTS, how do I choose the TENANT from the USER?

Am I doing the right thing when I first want to go through all the roles and find the right one?
Then add the selected ones to the new list.
Then go through this sheet and output the one that the user requested by ID (here you need help).

I had this option:

@GetMapping(&quot;/{id}&quot;)
    @PreAuthorize(&quot;hasAuthority(&#39;user:read&#39;)&quot;)
    public User landlordGet(@PathVariable(value = &quot;id&quot;) Long id) {
        List&lt;User&gt; sort = userRepository.findAll();
        List&lt;User&gt; res = new ArrayList&lt;&gt;();
        for (User us: sort) {
            if (us.getRole().equals(Role.TENANT)){
                res.add(us);
            }
        }
        ...

        return user;
    }

答案1

得分: 2

> 当我想首先浏览所有角色并找到合适的角色时,我是否在做正确的事情?然后将选定的角色添加到新列表中。然后浏览此表格,并输出用户按ID请求的角色(这里您需要帮助)。

不,实际上并不是,正如评论中已经提到的,在SQL / 数据库级别上可以完成很多工作。

注意: 您应该从 userRepository.findByRole(Role.TENANT); 获取一个 List&lt;User&gt;

由于 id 是作为请求的参数传递的,不清楚您在这里想要实现什么。

如果我理解正确,您想要执行类似以下的操作:<BR>
获取具有给定 id 的用户,并检查其角色是否为 TENANT。

更新:

如果您想先按角色进行,然后再按 id 进行筛选,可以尝试:

List&lt;User&gt; users = userRepository.findByRole(Role.TENANT);
Optional&lt;User&gt; optionalUser = users.stream().filter(user -&gt; user.getId().equals(id)).findFirst();
// 检查是否存在,或处理缺失的情况

更新 2:

所有租户:

List&lt;User&gt; users = userRepository.findByRole(Role.TENANT);

具有给定 id 的用户和可能的租户:

List&lt;User&gt; users = userRepository.findByRole(Role.TENANT);
Optional&lt;User&gt; optionalUser = users.stream().filter(user -&gt; user.getId().equals(id)).findFirst();
// 检查是否存在,或处理缺失的情况
if (optionalUser.isPresent()) {
  // 存在具有给定 id 的租户用户 - 进行处理
}
英文:

> Am I doing the right thing when I first want to go through all the roles and find the right one? Then add the selected ones to the new list. Then go through this sheet and output the one that the user requested by ID (here you need help).

Nope, not really, too much work that can be done in the SQL / database level as already mentioned in the comments.

Note: You should get a List&lt;User&gt; from userRepository.findByRole(Role.TENANT);

Since an id is coming from a request as a parameter, it is not clear what you want to achieve here.

If I understood correctly, then you want to go something like: <BR>
Get the User with the given id and check the role if it is a TENANT or not.

UPDATE:

If you wanna go with roles first and then filter by id, then try:

List&lt;User&gt; users = userRepository.findByRole(Role.TENANT);
Optional&lt;User&gt; optionalUser = users.stream().filter(user -&gt; user.getId().equals(id)).findFirst();
// Check for present or handle the missing case

UPDATE 2:

All tenants:

List&lt;User&gt; users = userRepository.findByRole(Role.TENANT);

User with the given id and possible tenant:

List&lt;User&gt; users = userRepository.findByRole(Role.TENANT);
Optional&lt;User&gt; optionalUser = users.stream().filter(user -&gt; user.getId().equals(id)).findFirst();
// Check for present or handle the missing case
if (optionalUser.isPresent()) {
  // tenant user with given id exists - handle it
}

huangapple
  • 本文由 发表于 2020年9月29日 04:29:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64109204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定