aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/EntitiesExtensions/User.cs
blob: 62614743c5e224cb3abce4d56232ed5a58ee1419 (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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Enumerations;

namespace Tango.BL.Entities
{
    public partial class User
    {
        /// <summary>
        /// Determines whether the user has the specified permission.
        /// </summary>
        /// <param name="permission">The permission.</param>
        /// <returns>
        ///   <c>true</c> if the user has permission; otherwise, <c>false</c>.
        /// </returns>
        public bool HasPermission(Permissions permission)
        {
            return UsersRoles.Select(x => x.Role).ToList().SelectMany(x => x.RolesPermissions).ToList().Exists(x => x.Permission.Code == permission.ToInt32());
        }

        /// <summary>
        /// Gets the aggregated user roles as enumerations.
        /// </summary>
        [NotMapped]
        public List<Role> Roles
        {
            get { return UsersRoles.Select(x => x.Role).ToList(); }
        }

        /// <summary>
        /// Gets the aggregated user permissions as enumerations.
        /// </summary>
        [NotMapped]
        public List<Permission> Permissions
        {
            get
            {
                return UsersRoles.Select(x => x.Role).ToList().SelectMany(x => x.RolesPermissions).Select(x => x.Permission).ToList();
            }
        }
    }
}