Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 3c161f5b

Přidáno uživatelem Vojtěch Bartička před asi 2 roky(ů)

Added ERole check to [Authorize] attribute

[Authorize] has ERole.ANNOTATOR as default, ERole can be specified as a parameter

Zobrazit rozdíly:

Backend/Backend/Authentication/AuthorizationAttribute.cs
1 1
using Core.Entities;
2
using Core.Enums;
2 3
using Microsoft.AspNetCore.Authorization;
3 4
using Microsoft.AspNetCore.Mvc;
4 5
using Microsoft.AspNetCore.Mvc.Filters;
......
8 9
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
9 10
public class AuthorizeAttribute : Attribute, IAuthorizationFilter
10 11
{
12
    private readonly ERole MinimumRequiredPermissions;
13

  
14
    public AuthorizeAttribute(ERole role = ERole.ANNOTATOR)
15
    {
16
        MinimumRequiredPermissions = role;
17
    }
18

  
19
    public AuthorizeAttribute()
20
    {
21
        MinimumRequiredPermissions = ERole.ANNOTATOR;
22
    }
23

  
11 24
    public void OnAuthorization(AuthorizationFilterContext context)
12 25
    {
13 26
        // skip authorization if action is decorated with [AllowAnonymous] attribute
......
16 29
            return;
17 30

  
18 31
        // authorization
19
        var user = (User?) context.HttpContext.Items["User"];
32
        var user = (User?)context.HttpContext.Items["User"];
20 33
        if (user == null)
21
            context.Result = new JsonResult(new {message = "Unauthorized"})
22
                {StatusCode = StatusCodes.Status401Unauthorized};
34
        {
35
            context.Result = new JsonResult(new { message = "Unauthorized" })
36
            {
37
                StatusCode = StatusCodes.Status401Unauthorized
38
            };
39
        }
40

  
41
        if (MinimumRequiredPermissions > user.Role)
42
        {
43
            context.Result = new JsonResult(new { message = "Unauthorized" })
44
            {
45
                StatusCode = StatusCodes.Status401Unauthorized
46
            };
47
        }
48

  
23 49
    }
24 50
}
Backend/Backend/Controllers/DocumentController.cs
6 6
using ILogger = Serilog.ILogger;
7 7
using Models.Documents;
8 8
using RestAPI.Exceptions;
9
using RestAPI.Authentication;
9 10

  
10 11
namespace RestAPI.Controllers;
11 12

  
......
27 28
    [ProducesResponseType((int)HttpStatusCode.OK)]
28 29
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
29 30
    [ProducesResponseType((int)HttpStatusCode.InternalServerError)]
31
    [Authorize(Core.Enums.ERole.ADMINISTRATOR)]
30 32
    public ActionResult<DocumentListResponse> GetDocuments([FromServices] ClientInfo clientInfo, [FromBody] DocumentListRequest documentListRequest)
31 33
    {
32 34
        if (clientInfo.LoggedUser == null)
......
35 37
            return Problem();
36 38
        }
37 39

  
38
        if (clientInfo.LoggedUser.Role != Core.Enums.ERole.ADMINISTRATOR)
39
        {
40
            return Forbid("User is not administrator");
41
        }
42

  
43 40
        return documentService.GetDocuments(documentListRequest);
44 41
    }
45 42

  
......
48 45
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
49 46
    [ProducesResponseType((int)HttpStatusCode.InternalServerError)]
50 47
    [ProducesResponseType((int)HttpStatusCode.UnsupportedMediaType)]
48
    [Authorize(Core.Enums.ERole.ADMINISTRATOR)]
51 49
    public ActionResult PostDocuments([FromServices] ClientInfo clientInfo, [FromBody] DocumentAddRequest documentAddRequest)
52 50
    {
53 51
        if (clientInfo.LoggedUser == null)
......
56 54
            return Problem();
57 55
        }
58 56

  
59
        if (clientInfo.LoggedUser.Role != Core.Enums.ERole.ADMINISTRATOR)
60
        {
61
            return Forbid("User is not administrator");
62
        }
63

  
64 57
        try
65 58
        {
66 59
            documentService.AddDocuments(documentAddRequest, clientInfo.LoggedUser.Id);

Také k dispozici: Unified diff