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

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

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.
public string GetUserTeam(int user_id)
{
    var user = _dbContext.Users.FirstOrDefault(u => u.user_id == user_id);
    if (user != null)
    {
        var team = _dbContext.Teams.FirstOrDefault(t => t.team_id == user.user_team);
        if (team != null)
        {
            return team.team_name;
        }
    }
    // Handle the case where user or team is not found.
    return string.Empty;
}
  1. Use the retrieved team name to filter your data.
public IList<object> TeamFilter(IList<object> inputList, int user_id)
{
    var userTeam = GetUserTeam(user_id);
    if (string.IsNullOrEmpty(userTeam))
    {
        // Handle the case where the user or team is not found.
        return new List<object>();
    }

    // Filter the inputList based on the user's team.
    var filteredList = inputList.Where(item => IsItemVisibleToUser(item, userTeam)).ToList();

    return filteredList;
}

private bool IsItemVisibleToUser(object item, string userTeam)
{
    // Implement logic to determine if the item is visible to the user based on their team.
    // You can use the userTeam and any relevant properties of the item to make this decision.
    // Return true if the item is visible, false otherwise.
    // Example:
    // var dataSetting = item as DataSetting;
    // return dataSetting != null && dataSetting.TeamName == userTeam;
}
  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:

public class Team
{
    [Key]
    public int team_id { get; set; }
    public string team_name { get; set; } = null!;
}

Here is my user class:

public class User
{
    [Key]
    public int user_id { get; set; }
    public string user_name { get; set; } = null!;
    public string user_pass { get; set; } = null!;
    public string user_mail { get; set; } = null!;
    public string user_type { get; set; } = null!;
    public int user_team { get; set; }
}

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:

public interface IUser
{
    public int reference_user_id { get; set; }
}

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

public class DataSetting : IUser
{
    [Key]
    public int datasetting_id { get; set; }
    public string datasetting_name { get; set; } = null!;
    public int reference_user_id { get; set; }
}

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:

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

Is there any idea how I can archive that?

答案1

得分: 1

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

public List<T> TeamFilter<T>(List<T> inputlist, int user_id) where T : IUser
{
    User? requesting_user = users.Where(x => x.user_id == user_id).FirstOrDefault();
    if (requesting_user != null)
    {
        Team? requesting_team = teams.Where(x => x.team_id == requesting_user.user_team).FirstOrDefault();
        if (requesting_team != null)
        {
            int requesting_team_id = requesting_team.team_id;

            List<T> outputList = inputlist
                .Join(users,
                    input => input.reference_user_id,
                    user => user.user_id,
                    (input, user) => (input, user)
                )
                .Where(x => x.user.user_team == requesting_team_id)
                .Select(x => x.input)
                .ToList();

            return outputList;
        }
        else
        { throw new ArgumentNullException(); }
    }
    else
    { throw new ArgumentNullException(); }
}
英文:

Found out that this can be implemented like the following:

public List&lt;T&gt; TeamFilter&lt;T&gt;(List&lt;T&gt; inputlist, int user_id) where T : IUser
{
	User? requesting_user = users.Where(x =&gt; x.user_id == user_id).FirstOrDefault();
	if (requesting_user != null)
	{
		Team? requesting_team = teams.Where(x =&gt; x.team_id == requesting_user.user_team).FirstOrDefault();
		if (requesting_team != null)
		{
			int requesting_team_id = requesting_team.team_id;

			List&lt;T&gt; outputList = inputlist
				.Join(users,
				  input =&gt; input.reference_user_id,
				  user =&gt; user.user_id,
				  (input, user) =&gt; (input, user)
				  )
				.Where(x =&gt; x.user.user_team == requesting_team_id)
				.Select(x =&gt; x.input)
				.ToList();

			return outputList;
		}
		else
		{ throw new ArgumentNullException(); }
	}
	else
	{ throw new ArgumentNullException(); }
}

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:

确定