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

Added user service that returns User Groups in the Claim #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Our.Umbraco.AuthU/Our.Umbraco.AuthU.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@
<Compile Include="Models\UmbracoKeyValue.cs" />
<Compile Include="OAuthConstants.cs" />
<Compile Include="OAuthContext.cs" />
<Compile Include="Services\UmbracoUsersRoleOAuthUserService.cs" />
<Compile Include="Services\MembershipProviderOAuthUserService.cs" />
<Compile Include="Web\Helpers\PrincipalHelper.cs" />
<Compile Include="Web\Mvc\AddOAuthChallengeResult.cs" />
Expand Down
65 changes: 65 additions & 0 deletions src/Our.Umbraco.AuthU/Services/UmbracoUsersRoleOAuthUserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Web.Security;
using Our.Umbraco.AuthU.Interfaces;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services;
using Umbraco.Core.Composing;

namespace Our.Umbraco.AuthU.Services
{
public abstract class UmbracoUsersRoleOAuthUserService : IOAuthUserService
{
public string UserType => "UmbracoUser";
private MembershipProvider MemberProvider => Membership.Providers["UsersMembershipProvider"];
private readonly IUserService _userService = Current.Services.UserService;

public bool ValidateUser(string username)
{
try
{
var user = _userService.GetByUsername(username);
return user != null && user.IsApproved && !user.IsLockedOut;
}
catch
{
return false;
}
}

public bool ValidateUser(string username, string password)
{
try
{
return MemberProvider.ValidateUser(username, password);
}
catch
{
return false;
}
}

public IEnumerable<Claim> GetUserClaims(string username)
{
IUser user = null;

try
{
user = _userService.GetByUsername(username);
}
catch { }

if (user != null)
{
yield return new Claim(ClaimTypes.NameIdentifier, user.ProviderUserKey.ToString());

var roles = user.Groups.Select(g => g.Alias);
foreach (var role in roles)
{
yield return new Claim(ClaimTypes.Role, role);
}
}
}
}
}