Microsoft Blazor用户基于用户团队的授权

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

Microsoft Blazor User Authorization based on user team

问题

I understand that you're looking for help with implementing security roles based on a user's team in your Microsoft Blazor Application. You want to filter data based on the user's team. Here's a basic outline of what you can do:

  1. Retrieve the user's team based on their user_id.
  1. public string GetUserTeam(int user_id)
  2. {
  3. var user = _dbContext.Users.FirstOrDefault(u => u.user_id == user_id);
  4. if (user != null)
  5. {
  6. var team = _dbContext.Teams.FirstOrDefault(t => t.team_id == user.user_team);
  7. if (team != null)
  8. {
  9. return team.team_name;
  10. }
  11. }
  12. // Handle the case where user or team is not found.
  13. return string.Empty;
  14. }
  1. Use the retrieved team name to filter your data.
  1. public IList<object> TeamFilter(IList<object> inputList, int user_id)
  2. {
  3. var userTeam = GetUserTeam(user_id);
  4. if (string.IsNullOrEmpty(userTeam))
  5. {
  6. // Handle the case where the user or team is not found.
  7. return new List<object>();
  8. }
  9. // Filter the inputList based on the user's team.
  10. var filteredList = inputList.Where(item => IsItemVisibleToUser(item, userTeam)).ToList();
  11. return filteredList;
  12. }
  13. private bool IsItemVisibleToUser(object item, string userTeam)
  14. {
  15. // Implement logic to determine if the item is visible to the user based on their team.
  16. // You can use the userTeam and any relevant properties of the item to make this decision.
  17. // Return true if the item is visible, false otherwise.
  18. // Example:
  19. // var dataSetting = item as DataSetting;
  20. // return dataSetting != null && dataSetting.TeamName == userTeam;
  21. }
  1. Call the TeamFilter function whenever you need to filter data for a specific user.

This is a simplified example to get you started. You'll need to implement the IsItemVisibleToUser function with your specific logic for determining if an item is visible to a user based on their team. Additionally, you may need to adjust the data model and relationships to suit your application's structure.

英文:

I have a Microsoft Blazor Application which has many entities and therefore each entity has each one razor component, interface, controller and service.

Now I need to implement security roles based on user's team, for example, a user belonging into "Greece" team should see only data created by users belonging into "Greece" team with type Member, of course there will be cases where a user with type Admin could see everything. But let's leave the User Type outside for a moment.

Here is my team class:

  1. public class Team
  2. {
  3. [Key]
  4. public int team_id { get; set; }
  5. public string team_name { get; set; } = null!;
  6. }

Here is my user class:

  1. public class User
  2. {
  3. [Key]
  4. public int user_id { get; set; }
  5. public string user_name { get; set; } = null!;
  6. public string user_pass { get; set; } = null!;
  7. public string user_mail { get; set; } = null!;
  8. public string user_type { get; set; } = null!;
  9. public int user_team { get; set; }
  10. }

I am trying to find the best/correct/elegant solution, so for example could be that in each List<object> getFunction(), I can pass the user id and make global function which will filter the sql results based on other ids belonging to the same team, but I have to call that function to all get methods and I have also to pass the user_id to all functions like add or edit.

Currently I have an Interface which all of my entities are inheriting:

  1. public interface IUser
  2. {
  3. public int reference_user_id { get; set; }
  4. }

Here is an example of an entity I need to filter:

  1. public class DataSetting : IUser
  2. {
  3. [Key]
  4. public int datasetting_id { get; set; }
  5. public string datasetting_name { get; set; } = null!;
  6. public int reference_user_id { get; set; }
  7. }

So I need a function that takes as input a list which will have reference_user_id as an attribute and matches the user, something like this:

  1. public IList&lt;object&gt; TeamFilter(IList&lt;object&gt; inputlist, int user_id)
  2. {
  3. teams = _dbContext.Teams.ToList();
  4. users = _dbContext.Users.ToList();
  5. }

Is there any idea how I can archive that?

答案1

得分: 1

我发现这可以像以下这样实现:

  1. public List<T> TeamFilter<T>(List<T> inputlist, int user_id) where T : IUser
  2. {
  3. User? requesting_user = users.Where(x => x.user_id == user_id).FirstOrDefault();
  4. if (requesting_user != null)
  5. {
  6. Team? requesting_team = teams.Where(x => x.team_id == requesting_user.user_team).FirstOrDefault();
  7. if (requesting_team != null)
  8. {
  9. int requesting_team_id = requesting_team.team_id;
  10. List<T> outputList = inputlist
  11. .Join(users,
  12. input => input.reference_user_id,
  13. user => user.user_id,
  14. (input, user) => (input, user)
  15. )
  16. .Where(x => x.user.user_team == requesting_team_id)
  17. .Select(x => x.input)
  18. .ToList();
  19. return outputList;
  20. }
  21. else
  22. { throw new ArgumentNullException(); }
  23. }
  24. else
  25. { throw new ArgumentNullException(); }
  26. }
英文:

Found out that this can be implemented like the following:

  1. public List&lt;T&gt; TeamFilter&lt;T&gt;(List&lt;T&gt; inputlist, int user_id) where T : IUser
  2. {
  3. User? requesting_user = users.Where(x =&gt; x.user_id == user_id).FirstOrDefault();
  4. if (requesting_user != null)
  5. {
  6. Team? requesting_team = teams.Where(x =&gt; x.team_id == requesting_user.user_team).FirstOrDefault();
  7. if (requesting_team != null)
  8. {
  9. int requesting_team_id = requesting_team.team_id;
  10. List&lt;T&gt; outputList = inputlist
  11. .Join(users,
  12. input =&gt; input.reference_user_id,
  13. user =&gt; user.user_id,
  14. (input, user) =&gt; (input, user)
  15. )
  16. .Where(x =&gt; x.user.user_team == requesting_team_id)
  17. .Select(x =&gt; x.input)
  18. .ToList();
  19. return outputList;
  20. }
  21. else
  22. { throw new ArgumentNullException(); }
  23. }
  24. else
  25. { throw new ArgumentNullException(); }
  26. }

huangapple
  • 本文由 发表于 2023年5月29日 17:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356065.html
匿名

发表评论

匿名网友

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

确定