Skip to content

Commit 9eee50a

Browse files
committed
Added models and db initializer
1 parent ea09dff commit 9eee50a

29 files changed

+750
-269
lines changed

client/package-lock.json

Lines changed: 51 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"@testing-library/react": "^9.3.2",
88
"@testing-library/user-event": "^7.1.2",
99
"axios": "^0.19.2",
10-
"react": "^16.13.1",
11-
"react-dom": "^16.13.1",
12-
"react-hook-form": "^5.3.1",
13-
"react-hot-loader": "^4.12.20",
14-
"react-redux": "^7.2.0",
15-
"react-router-dom": "^5.1.2",
10+
"react": "^16.14.0",
11+
"react-dom": "^16.14.0",
12+
"react-hook-form": "^5.7.2",
13+
"react-hot-loader": "^4.13.0",
14+
"react-redux": "^7.2.2",
15+
"react-router-dom": "^5.2.0",
1616
"react-scripts": "3.4.1",
1717
"redux": "^4.0.5",
1818
"redux-promise": "^0.6.0",

client/src/App.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,27 @@ import { PostDetail, PostCreate, PostEdit, PostDelete, Posts, Home } from "./com
77
import { Login } from "./components/Login";
88
import { Register } from "./components/Register";
99
import { Constants } from "./constants";
10+
import { usePermission, checkPermission } from "./hooks/usePermission.js";
1011

1112
export const PrivateRoute = ({ component: Component, ...rest }) => {
1213

1314
const userContext = useSelector(state => {
1415
return state.userContext;
1516
});
1617

18+
const isOk = () => {
19+
const ok = userContext.isAuthenticated;
20+
console.log('isOk', ok);
21+
return ok;
22+
};
23+
24+
const isAllowed = checkPermission(Component.name, userContext);
25+
26+
console.log('isAllowed', isAllowed);
27+
1728
return (
1829
<Route {...rest} render={props => {
19-
return (userContext.isAuthenticated)
30+
return (isAllowed)
2031
? <Component {...props} />
2132
: <Redirect to={{ pathname: '/login', state: { from: props.location } }} />;
2233
}} />

client/src/constants/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const Constants = {
33
LOGIN_SUCCESS: 'LOGIN_REQUEST_SUCCESS',
44
LOGIN_FAILURE: 'LOGIN_REQUEST_FAILURE',
55
LOGOUT_REQUEST: 'LOGOUT_REQUEST',
6+
PERMISSION_SUCCESS: 'PERMISSION_SUCCESS',
67

78
REGISTER_REQUEST: 'REGISTER_REQUEST',
89
REGISTER_SUCCESS: 'REGISTER_REQUEST_SUCCESS',

client/src/hooks/usePermission.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useState, useEffect } from "react";
2+
import { useSelector, useDispatch } from 'react-redux';
3+
4+
export const usePermission = (name) => {
5+
const [isAllowed, setIsAllowed] = useState(false);
6+
7+
const userContext = useSelector(state => {
8+
return state.userContext;
9+
});
10+
11+
const isOk = () => {
12+
const ok = userContext.isAuthenticated && userContext.role != null;
13+
console.log('isOk', ok);
14+
return ok;
15+
};
16+
17+
setIsAllowed(isOk());
18+
19+
return isAllowed;
20+
}
21+
22+
export const checkPermission = (name, userContext) => {
23+
const ok = userContext.isAuthenticated;
24+
console.log('checkPermission', name, userContext, ok);
25+
return ok;
26+
}

client/src/reducers/userReducer.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const initialState = {
66
token: null,
77
isRegistered: false,
88
error: null,
9+
role: null,
10+
resources: null
911
}
1012

1113
export default (state = initialState, action) => {
@@ -18,6 +20,7 @@ export default (state = initialState, action) => {
1820
isAuthenticated: true,
1921
user: { username: data.userName },
2022
token: data.access_token,
23+
role: data.role
2124
};
2225
case Constants.LOGOUT_REQUEST:
2326
localStorage.removeItem('data');
@@ -26,12 +29,11 @@ export default (state = initialState, action) => {
2629
user: initialState.user,
2730
token: initialState.token,
2831
};
29-
// case Constants.REGISTER_REQUEST:
30-
// return {
31-
// ...state,
32-
// isRegistered: initialState.isRegistered,
33-
// error: initialState.error,
34-
// };
32+
case Constants.PERMISSION_SUCCESS:
33+
return {
34+
...state,
35+
resources: data.resources
36+
};
3537
case Constants.REGISTER_SUCCESS:
3638
console.log('REGISTER_SUCCESS', action.payload);
3739
return {

server/AuthWebApplication/AuthWebApplication/AuthWebApplication.csproj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.3" />
11-
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.3" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
14-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
10+
<PackageReference Include="BizBook.Common.Library.EntityFrameworkCore" Version="1.1.1" />
11+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.9" />
12+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.9" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.9" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.9" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.9">
1516
<PrivateAssets>all</PrivateAssets>
1617
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1718
</PackageReference>
18-
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.5.1" />
19+
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" />
1920
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
20-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
21-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.5.1" />
21+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
22+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
2223
</ItemGroup>
2324

2425

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using AuthWebApplication.Models;
6+
using AuthWebApplication.Models.Db;
7+
using Microsoft.AspNetCore.Mvc;
8+
9+
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
10+
11+
namespace AuthWebApplication.Controllers
12+
{
13+
[Route("api/[controller]")]
14+
[ApiController]
15+
public class ApplicationRoleController : ControllerBase
16+
{
17+
private SecurityDbContext db;
18+
19+
public ApplicationRoleController(SecurityDbContext db)
20+
{
21+
this.db = db;
22+
}
23+
24+
// GET: api/<ApplicationRoleController>
25+
[HttpGet]
26+
public IActionResult Get()
27+
{
28+
var roles = db.ApplicationRoles.ToList();
29+
return Ok(roles);
30+
}
31+
32+
// GET api/<ApplicationRoleController>/5
33+
[HttpGet("{id}")]
34+
public IActionResult Get(string id)
35+
{
36+
var role = db.ApplicationRoles.Find(id);
37+
if (role != null)
38+
{
39+
return Ok(role);
40+
}
41+
42+
return NotFound();
43+
}
44+
45+
// POST api/<ApplicationRoleController>
46+
[HttpPost]
47+
public IActionResult Post([FromBody] ApplicationRole role)
48+
{
49+
if (string.IsNullOrWhiteSpace(role.Name))
50+
{
51+
return BadRequest();
52+
}
53+
54+
var entry = db.ApplicationRoles.Add(role);
55+
db.SaveChanges();
56+
return Ok();
57+
}
58+
59+
// PUT api/<ApplicationRoleController>/5
60+
[HttpPut("{id}")]
61+
public IActionResult Put(string id, [FromBody] string value)
62+
{
63+
var role = db.ApplicationRoles.Find(id);
64+
if (role!=null)
65+
{
66+
role.Name = value;
67+
db.SaveChanges();
68+
return Ok();
69+
}
70+
71+
return NotFound();
72+
}
73+
74+
// DELETE api/<ApplicationRoleController>/5
75+
[HttpDelete("{id}")]
76+
public IActionResult Delete(int id)
77+
{
78+
var role = db.ApplicationRoles.Find(id);
79+
if (role != null)
80+
{
81+
db.ApplicationRoles.Remove(role);
82+
db.SaveChanges();
83+
return Ok();
84+
}
85+
86+
return NotFound();
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)