Skip to content

Commit 700b95d

Browse files
committed
Added Permission List in view
1 parent a8d7871 commit 700b95d

File tree

6 files changed

+106
-5
lines changed

6 files changed

+106
-5
lines changed

client/src/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { } from 'react';
1+
import React, { } from 'react';
22
import './App.css';
33
import { BrowserRouter as Router, Switch, Route, Link, Redirect, useHistory } from "react-router-dom";
44
import { useSelector, useDispatch } from 'react-redux';
@@ -8,7 +8,7 @@ import { Register } from "./components/Register";
88
import { Constants } from "./constants";
99
import { checkPermission } from "./utils/permissionManager.js";
1010
import { ResourceCreate, ResourceList } from "./components/Resource";
11-
import { PermissionCreate } from "./components/Permission";
11+
import { PermissionCreate, PermissionList } from "./components/Permission";
1212
import { RoleCreate } from './components/Role';
1313

1414
export const PrivateRoute = ({ component: Component, name: resource, ...rest }) => {
@@ -76,6 +76,7 @@ const App = () => {
7676
{ name: 'link-posts', url: '/posts', text: 'Posts', component: Posts },
7777
{ name: 'link-post-create', url: '/post-create', text: 'Create post', component: PostCreate },
7878
{ name: 'link-permission-create', url: '/permission-create', text: 'Create permission', component: PermissionCreate },
79+
{ name: 'link-permission-list', url: '/permission-list', text: 'List permissions', component: PermissionList },
7980
{ name: 'link-role-create', url: '/role-create', text: 'Create role', component: RoleCreate },
8081
{ name: 'link-resource-create', url: '/resource-create', text: 'Create resource', component: ResourceCreate },
8182
{ name: 'link-resource-list', url: '/resource-list', text: 'List resource', component: ResourceList },

client/src/components/Permission.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Formik, Form, Field, ErrorMessage } from 'formik';
44
import * as Yup from 'yup';
55
import { useSelector, useDispatch } from 'react-redux';
66
import { useEffect } from 'react';
7+
import { Table, Row, Col } from 'react-bootstrap';
78

89
const PermissionSchema = Yup.object().shape({
910
resourceId: Yup.string()
@@ -89,3 +90,53 @@ export const PermissionCreate = () => {
8990
</div >
9091
);
9192
};
93+
94+
95+
export const PermissionList = () => {
96+
97+
let dispatch = useDispatch();
98+
99+
const permissionContext = useSelector(state => {
100+
return state.permissionContext;
101+
});
102+
103+
useEffect(() => {
104+
dispatch({ type: "FETCH_PERMISSION" });
105+
}, []);
106+
107+
return (
108+
<>
109+
<Row>
110+
<h2>Permission List {permissionContext.permissionList.length}</h2>
111+
</Row>
112+
<Row>
113+
<Col>
114+
<Table striped bordered hover>
115+
<thead>
116+
<tr>
117+
<th>#</th>
118+
<th>Resource</th>
119+
<th>Role</th>
120+
<th>Is allowed</th>
121+
</tr>
122+
</thead>
123+
<tbody>
124+
{
125+
permissionContext.permissionList.map((resource, index) => {
126+
return (
127+
<tr>
128+
<td>{index + 1}</td>
129+
<td>{resource.resourceName}</td>
130+
<td>{resource.roleName}</td>
131+
<td>{resource.isAllowed.toString()}</td>
132+
</tr>
133+
)
134+
})
135+
}
136+
</tbody>
137+
</Table>
138+
</Col>
139+
</Row>
140+
</>
141+
);
142+
}

client/src/components/Posts.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ import React, { useState, useEffect } from 'react';
22
import { Switch, Route, Link, useRouteMatch, useParams, useHistory } from "react-router-dom";
33
import { useForm } from 'react-hook-form';
44
import { useSelector, useDispatch } from 'react-redux';
5+
import { checkPermission } from "../utils/permissionManager.js";
6+
7+
export const SecuedLink = ({ resource, text, url }) => {
8+
9+
const userContext = useSelector(state => {
10+
return state.userContext;
11+
});
12+
13+
console.log('SecuedLink ', resource, text, url);
14+
15+
const isAllowed = checkPermission(resource, userContext);
16+
17+
return (isAllowed && <Link to={() => url}>{text}</Link>)
18+
}
519

620
export const Home = () => {
721
return (
@@ -188,6 +202,7 @@ export const PostSummary = (post) => {
188202
<Link to={() => `/post-detail/${post.id}`}>Detail</Link> &nbsp;
189203
<Link to={() => `/post-edit/${post.id}`}>Edit</Link> &nbsp;
190204
<Link to={() => `/post-delete/${post.id}`}>Delete</Link> &nbsp;
205+
<SecuedLink resource='link-post-delete' url={ `/post-delete/${post.id}`} text='Delete'></SecuedLink>
191206
</div>
192207
)
193208
}

client/src/components/Resource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export const ResourceList = () => {
8383
{
8484
resourceContext.resourceList.map((resource, index) => {
8585
return (
86-
<tr>
86+
<tr key={index}>
8787
<td>{index + 1}</td>
8888
<td>{resource.name}</td>
8989
</tr>

server/AuthWebApplication/AuthWebApplication/Controllers/ApplicationPermissionsController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.EntityFrameworkCore;
88
using AuthWebApplication.Models;
99
using AuthWebApplication.Models.Db;
10+
using AuthWebApplication.Models.ViewModels;
1011

1112
namespace AuthWebApplication.Controllers
1213
{
@@ -23,9 +24,9 @@ public ApplicationPermissionsController(SecurityDbContext context)
2324

2425
// GET: api/ApplicationPermissions
2526
[HttpGet]
26-
public async Task<ActionResult<IEnumerable<ApplicationPermission>>> GetPermissions()
27+
public async Task<ActionResult<IEnumerable<ApplicationPermissionViewModel>>> GetPermissions()
2728
{
28-
return await _context.Permissions.ToListAsync();
29+
return await _context.Permissions.Include(x=>x.Role).Include(x=>x.Resource).Select(x=>new ApplicationPermissionViewModel(x)).ToListAsync();
2930
}
3031

3132
// GET: api/ApplicationPermissions/5
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace AuthWebApplication.Models.ViewModels
7+
{
8+
public class ApplicationPermissionViewModel
9+
{
10+
public ApplicationPermissionViewModel(ApplicationPermission permission)
11+
{
12+
this.Id = permission.Id;
13+
this.RoleId = permission.RoleId;
14+
this.ResourceId = permission.ResourceId;
15+
this.RoleId = permission.RoleId;
16+
this.RoleName = permission.Role.Name;
17+
this.ResourceName = permission.Resource.Name;
18+
this.IsAllowed = permission.IsAllowed.ToString();
19+
}
20+
21+
public string Id { get; set; }
22+
23+
public string RoleId { get; set; }
24+
25+
public string ResourceId { get; set; }
26+
27+
public string RoleName { get; set; }
28+
29+
public string ResourceName { get; set; }
30+
31+
public string IsAllowed { get; set; }
32+
}
33+
}

0 commit comments

Comments
 (0)