aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs
blob: cd2bdadff878761b11d30554b5e0c41eca694a0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using Microsoft.AspNetCore.Mvc;
using System;
using Timeline.Auth;
using Timeline.Services.User;

namespace Timeline.Controllers
{
    public static class ControllerAuthExtensions
    {
        public static bool UserHasPermission(this ControllerBase controller, UserPermission permission)
        {
            return controller.User.HasPermission(permission);
        }

        public static long GetUserId(this ControllerBase controller)
        {
            return controller.GetOptionalUserId() ?? throw new InvalidOperationException(Resource.ExceptionNoUserId);
        }

        public static long? GetOptionalUserId(this ControllerBase controller)
        {
            return controller.User.GetUserId();
        }
    }
}