-
Notifications
You must be signed in to change notification settings - Fork 175
/
SysUserPassword.cs
74 lines (64 loc) · 2.16 KB
/
SysUserPassword.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Piranha.Data;
namespace Piranha.Models
{
/// <summary>
/// The SysUserPassword record is used for changing a users password in the database.
/// </summary>
[Table(Name="sysuser"), PrimaryKey(Column="sysuser_id")]
public class SysUserPassword : GuidRecord<SysUserPassword>
{
#region Fields
/// <summary>
/// Gets/sets the id.
/// </summary>
[Column(Name="sysuser_id")]
public override Guid Id { get ; set ; }
/// <summary>
/// Gets/sets the password.
/// </summary>
[Column(Name="sysuser_password", OnLoad="OnPasswordLoad", OnSave="OnPasswordSave")]
public string Password { get ; set ; }
#endregion
#region Properties
/// <summary>
/// Gets/sets the confirmation password.
/// </summary>
[Compare("Password", ErrorMessage="Lösenorden matchar inte.")]
public string PasswordConfirm { get ; set ; }
/// <summary>
/// Checks weather the password is set and can be saved.
/// </summary>
public bool IsSet { get { return !String.IsNullOrEmpty(Password) ; } }
#endregion
/// <summary>
/// Generates a simple 8 character random password.
/// </summary>
/// <returns>The password</returns>
public static string GeneratePassword() {
Random rnd = new Random() ;
string sc = "!#%&/()=@$" ;
// Generate base password
string pw = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6) ;
// Insert two random special characters somewhere
pw = pw.Insert(rnd.Next() % pw.Length, sc.Substring(rnd.Next() % sc.Length, 1)) ;
pw = pw.Insert(rnd.Next() % pw.Length, sc.Substring(rnd.Next() % sc.Length, 1)) ;
return pw ;
}
/// <summary>
/// Saves the record to the database. Checks so empty passwords don't get saved.
/// </summary>
/// <param name="tx">Optional transaction</param>
/// <returns>Weather the operation succeeded or not</returns>
public override bool Save(System.Data.IDbTransaction tx = null) {
if (IsSet)
return base.Save(tx);
else return false ;
}
}
}