Ewl » Canonical The canonical version of EWL Read More
Clone URL:  
UserFieldTable.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System.Collections.Generic; using System.Linq; using System.Web.UI; using System.Web.UI.WebControls; using EnterpriseWebLibrary.Encryption; using EnterpriseWebLibrary.EnterpriseWebFramework.UserManagement; using EnterpriseWebLibrary.WebSessionState; using Humanizer; namespace EnterpriseWebLibrary.EnterpriseWebFramework.Controls { /// <summary> /// A table that contains fields to enable editing of a user's generic properties. /// NOTE: Convert this to use FormItems and take additional FormItems to allow customization of this control? /// </summary> public class UserFieldTable: WebControl { /// <summary> /// The validated email address. /// </summary> public readonly DataValue<string> Email = new DataValue<string>(); /// <summary> /// Only valid for systems which are forms authentication capable. /// </summary> public readonly DataValue<int> Salt = new DataValue<int>(); /// <summary> /// Only valid for systems which are forms authentication capable. /// </summary> public readonly DataValue<byte[]> SaltedPassword = new DataValue<byte[]>(); /// <summary> /// Only valid for systems which are forms authentication capable. /// </summary> public readonly DataValue<bool> MustChangePassword = new DataValue<bool>(); /// <summary> /// The validated role ID. /// </summary> public readonly DataValue<int> RoleId = new DataValue<int>(); private string passwordToEmail; /// <summary> /// Call this during LoadData. /// </summary> /// <param name="userId"></param> /// <param name="availableRoles">Pass a restricted list of <see cref="Role"/>s the user may select. Otherwise, Roles available /// in the System Provider are used.</param> public void LoadData( int? userId, List<Role> availableRoles = null ) { availableRoles = ( availableRoles?.OrderBy( r => r.Name ) ?? UserManagementStatics.SystemProvider.GetRoles() ).ToList(); var user = userId.HasValue ? UserManagementStatics.GetUser( userId.Value, true ) : null; var facUser = includePasswordControls() && user != null ? FormsAuthStatics.GetUser( user.UserId, true ) : null; var b = FormItemBlock.CreateFormItemTable( heading: "Security Information" ); b.AddFormItems( Email.ToEmailAddressControl( false, value: user != null ? user.Email : "" ).ToFormItem( label: "Email address".ToComponents() ) ); if( includePasswordControls() ) { var group = new LegacyRadioButtonGroup( false ); var keepPassword = FormItem.Create( "", group.CreateInlineRadioButton( true, label: userId.HasValue ? "Keep the current password" : "Do not create a password" ), validationGetter: control => new EwfValidation( ( pbv, validator ) => { if( !control.IsCheckedInPostBack( pbv ) ) return; if( user != null ) { Salt.Value = facUser.Salt; SaltedPassword.Value = facUser.SaltedPassword; MustChangePassword.Value = facUser.MustChangePassword; } else genPassword( false ); } ) ); var generatePassword = FormItem.Create( "", group.CreateInlineRadioButton( false, label: "Generate a " + ( userId.HasValue ? "new, " : "" ) + "random password and email it to the user" ), validationGetter: control => new EwfValidation( ( pbv, validator ) => { if( control.IsCheckedInPostBack( pbv ) ) genPassword( true ); } ) ); var providePasswordSelected = new DataValue<bool>(); var providePassword = FormItem.Create( "", group.CreateBlockRadioButton( false, label: "Provide a {0}".FormatWith( userId.HasValue ? "new password" : "password" ), validationMethod: ( postBackValue, validator ) => providePasswordSelected.Value = postBackValue.Value, nestedControlListGetter: () => { return FormState.ExecuteWithValidationPredicate( () => providePasswordSelected.Value, () => { var password = new DataValue<string>(); var newPasswordTable = EwfTable.Create( style: EwfTableStyle.StandardExceptLayout, classes: "newPassword".ToCollection() ); foreach( var i in password.GetPasswordModificationFormItems() ) newPasswordTable.AddItem( new EwfTableItem( i.Label, i.ToControl( omitLabel: true ) ) ); new EwfValidation( validator => { var p = new Password( password.Value ); Salt.Value = p.Salt; SaltedPassword.Value = p.ComputeSaltedHash(); MustChangePassword.Value = false; } ); return newPasswordTable.ToCollection(); } ); } ), validationGetter: control => control.Validation ); b.AddFormItems( FormItem.Create( "Password", ControlStack.CreateWithControls( true, keepPassword.ToControl(), generatePassword.ToControl(), providePassword.ToControl() ) ) ); } b.AddFormItems( FormItem.Create( "Role", SelectList.CreateDropDown( from i in availableRoles select SelectListItem.Create( i.RoleId as int?, i.Name ), user?.Role.RoleId ), validationGetter: control => new EwfValidation( ( pbv, validator ) => RoleId.Value = control.ValidateAndGetSelectedItemIdInPostBack( pbv, validator ) ?? default( int ) ) ) ); Controls.Add( b ); } private bool includePasswordControls() { return FormsAuthStatics.FormsAuthEnabled; } private void genPassword( bool emailPassword ) { var password = new Password(); Salt.Value = password.Salt; SaltedPassword.Value = password.ComputeSaltedHash(); MustChangePassword.Value = true; if( emailPassword ) passwordToEmail = password.PasswordText; } /// <summary> /// Call this during ModifyData. /// </summary> // NOTE SJR: This needs to change: You can't see this comment unless you're scrolling through all of the methods. It's easy to not call this // even though the radio button for generating a new password and emailing it to the user is always there. public void SendEmailIfNecessary() { if( passwordToEmail == null ) return; FormsAuthStatics.SendPassword( Email.Value, passwordToEmail ); EwfPage.AddStatusMessage( StatusMessageType.Info, "Password reset email sent." ); } /// <summary> /// Returns the div tag, which represents this control in HTML. /// </summary> protected override HtmlTextWriterTag TagKey => HtmlTextWriterTag.Div; } }