aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Net/AuthenticatedAttribute.cs
blob: 83e404dfcee4392696d6ae9b9445090a34adc540 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using ServiceStack.Web;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MediaBrowser.Controller.Net
{
    public class AuthenticatedAttribute : Attribute, IHasRequestFilter, IAuthenticated
    {
        public IAuthService AuthService { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether or not to allow local unauthenticated access.
        /// </summary>
        /// <value><c>true</c> if [allow local]; otherwise, <c>false</c>.</value>
        public bool AllowLocal { get; set; }

        /// <summary>
        /// Gets or sets the roles.
        /// </summary>
        /// <value>The roles.</value>
        public string Roles { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [escape parental control].
        /// </summary>
        /// <value><c>true</c> if [escape parental control]; otherwise, <c>false</c>.</value>
        public bool EscapeParentalControl { get; set; }

        /// <summary>
        /// The request filter is executed before the service.
        /// </summary>
        /// <param name="request">The http request wrapper</param>
        /// <param name="response">The http response wrapper</param>
        /// <param name="requestDto">The request DTO</param>
        public void RequestFilter(IRequest request, IResponse response, object requestDto)
        {
            AuthService.Authenticate(request, response, requestDto, this);
        }

        /// <summary>
        /// A new shallow copy of this filter is used on every request.
        /// </summary>
        /// <returns>IHasRequestFilter.</returns>
        public IHasRequestFilter Copy()
        {
            return this;
        }

        /// <summary>
        /// Order in which Request Filters are executed.
        /// &lt;0 Executed before global request filters
        /// &gt;0 Executed after global request filters
        /// </summary>
        /// <value>The priority.</value>
        public int Priority
        {
            get { return 0; }
        }


        public IEnumerable<string> GetRoles()
        {
            return (Roles ?? string.Empty).Split(',')
                .Where(i => !string.IsNullOrWhiteSpace(i));
        }
    }

    public interface IAuthenticated
    {
        bool EscapeParentalControl { get; }

        bool AllowLocal { get; }
        IEnumerable<string> GetRoles();
    }
}