auth improvements related to multi-tenancy
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Json;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Shared;
|
||||
@ -114,5 +115,42 @@ namespace Oqtane.Security
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ClaimsIdentity CreateClaimsIdentity(Alias alias, User user, List<UserRole> userroles)
|
||||
{
|
||||
user.Roles = "";
|
||||
foreach (UserRole userrole in userroles)
|
||||
{
|
||||
user.Roles += userrole.Role.Name + ";";
|
||||
}
|
||||
if (user.Roles != "") user.Roles = ";" + user.Roles;
|
||||
return CreateClaimsIdentity(alias, user);
|
||||
}
|
||||
|
||||
public static ClaimsIdentity CreateClaimsIdentity(Alias alias, User user)
|
||||
{
|
||||
ClaimsIdentity identity = new ClaimsIdentity(Constants.AuthenticationScheme);
|
||||
if (alias != null && user != null && !user.IsDeleted)
|
||||
{
|
||||
identity.AddClaim(new Claim(ClaimTypes.Name, user.Username));
|
||||
identity.AddClaim(new Claim(ClaimTypes.PrimarySid, user.UserId.ToString()));
|
||||
identity.AddClaim(new Claim(ClaimTypes.GroupSid, alias.AliasId.ToString()));
|
||||
if (user.Roles.Contains(RoleNames.Host))
|
||||
{
|
||||
// host users are site admins by default
|
||||
identity.AddClaim(new Claim(ClaimTypes.Role, RoleNames.Host));
|
||||
identity.AddClaim(new Claim(ClaimTypes.Role, RoleNames.Admin));
|
||||
identity.AddClaim(new Claim(ClaimTypes.Role, RoleNames.Registered));
|
||||
}
|
||||
foreach (string role in user.Roles.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
if (!identity.Claims.Any(item => item.Type == ClaimTypes.Role && item.Value == role))
|
||||
{
|
||||
identity.AddClaim(new Claim(ClaimTypes.Role, role));
|
||||
}
|
||||
}
|
||||
}
|
||||
return identity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user