Skip to content

Commit

Permalink
Add user to group on first login.
Browse files Browse the repository at this point in the history
  • Loading branch information
C0derPr0 committed Mar 13, 2021
1 parent 0741bbb commit 57406cf
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"appService.preDeployTask": "publish-release",
"appService.deploySubpath": "bin\\Release\\net5.0\\publish",
"appService.defaultWebAppToDeploy": "/subscriptions/84c290c7-23ff-45f5-9f6b-41a65b683505/resourceGroups/appsvc_windows_centralus/providers/Microsoft.Web/sites/mittbalans",
"azureFunctions.deploySubpath": "bin/Release/netcoreapp3.1/publish",
"azureFunctions.projectLanguage": "C#",
"azureFunctions.projectRuntime": "~3",
"debug.internalConsoleOptions": "neverOpen",
"azureFunctions.preDeployTask": "publish"
"azureFunctions.preDeployTask": "publish",
"appService.defaultWebAppToDeploy": "/subscriptions/84c290c7-23ff-45f5-9f6b-41a65b683505/resourceGroups/Claes/providers/Microsoft.Web/sites/balans-api"
}
10 changes: 0 additions & 10 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@
"type": "process",
"dependsOn": "clean release",
"problemMatcher": "$msCompile"
},
{
"type": "func",
"dependsOn": "build",
"options": {
"cwd": "${workspaceFolder}/bin/Debug/netcoreapp3.1"
},
"command": "host start",
"isBackground": true,
"problemMatcher": "$func-dotnet-watch"
}
]
}
6 changes: 6 additions & 0 deletions Books.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
<PackageReference Include="Newtonsoft.json" Version="12.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>
<ItemGroup>
<Content Remove="client\**" />
<Compile Remove="client\**" />
<EmbeddedResource Remove="client\**" />
<None Remove="client\**" />
</ItemGroup>
</Project>
27 changes: 27 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:20895/",
"sslPort": 44307
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Books": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
11 changes: 4 additions & 7 deletions api/Controllers/GroupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ namespace Books.Api.Controllers
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

using Books.Api.Models;
using Books.Api.Services;

[Route("api/[controller]")]
[ApiController]
public class GroupController : ControllerBase
{
private readonly GroupService _groupService;
private readonly Services.GroupService _groupService;

public GroupController(GroupService groupService)
public GroupController(Services.GroupService groupService)
{
_groupService = groupService;
}
Expand All @@ -21,7 +18,7 @@ public GroupController(GroupService groupService)
public ActionResult<List<Models.Group>> Get() =>
_groupService.Get();

[HttpGet("{id:length(36)}", Name = "GetUserGroups")]
[HttpGet("User/{id:length(36)}", Name = "GetUserGroups")]
public ActionResult<List<Models.Group>> Get(string id)
{
var groups = _groupService.Get(id);
Expand All @@ -34,7 +31,7 @@ public GroupController(GroupService groupService)
return groups;
}

[HttpGet("{id:length(24)}", Name = "GetUserGroup")]
[HttpGet("{id:length(24)}", Name = "GetGroup")]
public ActionResult<Models.Group> GetUserGroup(string id)
{
var group = _groupService.GetGroup(id);
Expand Down
7 changes: 5 additions & 2 deletions api/Services/GroupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ public Group Create(Models.Group group)
{
group.Id = MongoDB.Bson.ObjectId.GenerateNewId().ToString();

_groups.InsertOne(group);

if(_groups.CountDocuments(g => g.AzId == group.AzId && g.GroupName == group.GroupName) < 1)
{
_groups.InsertOne(group);
}

return group;
}
}
Expand Down
Binary file modified bin/Debug/net5.0/Books.dll
Binary file not shown.
Binary file modified bin/Debug/net5.0/Books.pdb
Binary file not shown.
Binary file modified bin/Debug/net5.0/ref/Books.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion client/public/signin-callback.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h1>Authentification callback processing...</h1>
//new Oidc.UserManager({ response_mode: "query" }).signinRedirectCallback().then(function () {
// Settings loadUserInfo: false because b2c does not currently support userinfo endpoint.
new Oidc.UserManager({ response_mode: "query", loadUserInfo: false }).signinRedirectCallback().then(function () {

window.location = "index.html";
}).catch(function (e) {
console.error(e);
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/BookContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const BookContent = () => {
const updateData = (id: any, updatedBook: any) => {
setEditing(false);

axios.put(`${Constants.apiRoot}Book/${id}`, updatedBook).then(function(test) {
axios.put(`${Constants.apiRoot}Book/${id}`, updatedBook).then(function(res) {
setBooks(books.map((book: { Id: any; Category: string; Name: string; Price: string; Author: string; }) => (book.Id === id ? updatedBook : book)))
});
}
Expand Down
52 changes: 38 additions & 14 deletions client/src/services/AuthService.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Log, User, UserManager, UserManagerSettings } from 'oidc-client';
import {Constants} from '../helpers/Constants';
import { Constants } from '../helpers/Constants';
import axios from 'axios';

export class AuthService {
public userManager: UserManager;

constructor() {
const settings = {
// This is the metadata endpoint
authority: `https://mittbalans.b2clogin.com/MittBalans.onmicrosoft.com/b2c_1_react_signup_signin/v2.0/.well-known/openid-configuration`,
// Turn off calls to user info since CORS will block it
loadUserInfo: false,
loadUserInfo: false,
// The URL where the Web UI receives the login result
redirect_uri: `http://localhost:48599/signin-callback.html`,
// The no longer recommended implicit flow must be used if CORS is disabled
Expand All @@ -19,32 +19,56 @@ export class AuthService {
// Other OAuth settings
client_id: Constants.azureApplicationId,
// openid is required, remove https://contoso.onmicrosoft.com/test/Read if access_token is not required.
scope: 'openid offline_access https://graph.microsoft.com/User.Read.All',
// Supply these details explicitly. Directly copied from azure ad b2c policy metadata endpoint.
metadata: {
scope: 'openid offline_access https://graph.microsoft.com/User.Read.All',
// Supply these details explicitly. Directly copied from azure ad b2c policy metadata endpoint.
metadata: {
issuer: `https://${Constants.azureTenantName}.b2clogin.com/${Constants.azureTenantId}/v2.0/`,
authorization_endpoint: `https://${Constants.azureTenantName}.b2clogin.com/${Constants.azureTenantName}.onmicrosoft.com/${Constants.azureSignUpSignInPolicy}/oauth2/v2.0/authorize`,
token_endpoint: `https://${Constants.azureTenantName}.b2clogin.com/${Constants.azureTenantName}.onmicrosoft.com/${Constants.azureSignUpSignInPolicy}/oauth2/v2.0/token`,
jwks_uri : `https://${Constants.azureTenantName}.b2clogin.com/${Constants.azureTenantName}.onmicrosoft.com/discovery/v2.0/keys?p=${Constants.azureSignUpSignInPolicy}`,
jwks_uri: `https://${Constants.azureTenantName}.b2clogin.com/${Constants.azureTenantName}.onmicrosoft.com/discovery/v2.0/keys?p=${Constants.azureSignUpSignInPolicy}`,
end_session_endpoint: `https://${Constants.azureTenantName}.b2clogin.com/${Constants.azureTenantName}.onmicrosoft.com/${Constants.azureSignUpSignInPolicy}/oauth2/v2.0/logout?post_logout_redirect_uri=${Constants.redirectRoot}`
},

} as UserManagerSettings;
this.userManager = new UserManager(settings);
} as UserManagerSettings;

this.userManager = new UserManager(settings);

Log.logger = console;
Log.level = Log.INFO;
}

public async getUser(): Promise<User | null> {
return await this.userManager.getUser();
var user = await this.userManager.getUser();

// Check if the user is new & assign him to the customer user group if he is not.
if (user != null) {
if (user.profile.newUser) {
var userGroups = await axios.get(`${Constants.apiRoot}Group/User/${user.profile.sub}`);

if(userGroups.data.length < 1) {
var userGroup = {
Id: '',
AzId: user.profile.sub,
GroupName: 'Customer'
}

axios.post(`${Constants.apiRoot}Group`, userGroup);
}
}
}

return user;
}

public async getGroup(user: User): Promise<string | null> {
var group = await axios.get(`${Constants.apiRoot}Group/${user.profile.sub}`)
var group = await axios.get(`${Constants.apiRoot}Group/User/${user.profile.sub}`)

if(group.data.length > 0) {

return group.data[0].GroupName;
}

return group.data[0].GroupName;
return null;
}

public login(): Promise<void> {
Expand Down
12 changes: 0 additions & 12 deletions obj/Debug/net5.0/Books.csproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,7 @@ D:\Projects\Web\ASP.Net\Core\Books\bin\Debug\net5.0\package-lock.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\api\Properties\launchSettings.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\appsettings.Development.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\appsettings.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\client\build\asset-manifest.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\client\build\manifest.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\client\package-lock.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\client\package.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\client\public\manifest.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\client\tsconfig.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\client\tslint.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\package-lock.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\react-oidc-client-js-master\src\package-lock.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\react-oidc-client-js-master\src\package.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\react-oidc-client-js-master\src\public\manifest.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\react-oidc-client-js-master\src\tsconfig.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\react-oidc-client-js-master\src\tslint.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\Books.exe
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\Books.deps.json
D:\Projects\Web\ASP.Net\Core\balans\bin\Debug\net5.0\Books.runtimeconfig.json
Expand Down
Binary file modified obj/Debug/net5.0/Books.dll
Binary file not shown.
Binary file modified obj/Debug/net5.0/Books.pdb
Binary file not shown.
Binary file modified obj/Debug/net5.0/ref/Books.dll
Binary file not shown.

0 comments on commit 57406cf

Please sign in to comment.