blob: f0bad3befab1b378284f01fbfe0a046f3710af44 (
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
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;
using SimpleValidator.Extensions;
using Newtonsoft.Json;
using System.Xml.Serialization;
using Tango.Core.Cryptography;
namespace Tango.BL.Entities
{
public partial class User : UserBase
{
private static IHashGenerator _hashGenerator;
/// <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());
}
public bool HasAnyPermission(IEnumerable<Permissions> permissions)
{
return UsersRoles.Select(x => x.Role).ToList().SelectMany(x => x.RolesPermissions).ToList().Any(x => permissions.Contains((Permissions)x.Permission.Code));
}
public bool HasAnyPermission(params Permissions[] permissions)
{
return HasAnyPermission(permissions.ToList());
}
/// <summary>
/// Determines whether the user has role.
/// </summary>
/// <param name="role">The role.</param>
public bool HasRole(Roles role)
{
return UsersRoles.ToList().Exists(x => x.Role.Code == role.ToInt32());
}
/// <summary>
/// Gets the aggregated user roles.
/// </summary>
[NotMapped]
[JsonIgnore]
public List<Role> Roles
{
get { return UsersRoles.Select(x => x.Role).ToList(); }
}
/// <summary>
/// Gets the aggregated FSE user roles.
/// </summary>
[NotMapped]
[JsonIgnore]
public List<Role> FSERoles
{
get { return UsersRoles.Select(x => x.Role).Where(x => x.Name.StartsWith("FSE")).ToList(); }
}
/// <summary>
/// Gets the aggregated user permissions as enumerations.
/// </summary>
[NotMapped]
[JsonIgnore]
public List<Permission> Permissions
{
get
{
return UsersRoles.Select(x => x.Role).ToList().SelectMany(x => x.RolesPermissions).Select(x => x.Permission).ToList();
}
}
protected override void RaisePropertyChanged(string propName)
{
base.RaisePropertyChanged(propName);
}
public override bool Validate(ObservablesContext context)
{
return base.Validate(context) && Contact.Validate(context) && Address.Validate(context);
}
protected override void OnValidating(ObservablesContext context)
{
base.OnValidating(context);
var users = context.Users.ToList();
if (users.Exists(x => x.Guid != Guid && x.Email.ToLower() == Email.ToLower()))
{
InsertError(nameof(Email), "The specified email is already taken by another account.");
}
if (!Email.IsEmail())
{
InsertError(nameof(Email), "The specified email address is invalid.");
}
}
/// <summary>
/// Gets the specified password hash.
/// </summary>
/// <param name="password">The password.</param>
/// <returns></returns>
public static String GetPasswordHash(String password)
{
return GetHashGenerator().Encrypt(password);
}
private static IHashGenerator GetHashGenerator()
{
if (_hashGenerator == null)
{
_hashGenerator = new BasicHashGenerator();
}
return _hashGenerator;
}
/// <summary>
/// Initializes a new instance of the <see cref="User" /> class.
/// </summary>
public User() : base()
{
}
}
}
|