@@ -19,15 +19,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
19
20
20
using DotNetNuke . Entities . Users ;
21
21
using DotNetNuke . Framework . JavaScriptLibraries ;
22
+ using DotNetNuke . Instrumentation ;
23
+ using DotNetNuke . Security . Membership ;
22
24
using DotNetNuke . Security . Permissions ;
23
25
using DotNetNuke . Security . Roles ;
24
26
using DotNetNuke . Services . Localization ;
25
27
using DotNetNuke . Web . Mvc . Framework . ActionFilters ;
26
28
using DotNetNuke . Web . Mvc . Framework . Controllers ;
27
29
using System ;
30
+ using System . Net ;
28
31
using System . Web . Mvc ;
32
+ using Upendo . Modules . UserManager . Models . DnnModel ;
29
33
using Upendo . Modules . UserManager . Utility ;
30
34
using Upendo . Modules . UserManager . ViewModels ;
35
+ using static Telerik . Web . UI . OrgChartStyles ;
31
36
32
37
namespace Upendo . Modules . UserManager . Controllers
33
38
{
@@ -122,21 +127,48 @@ public ActionResult Create(UserViewModel item)
122
127
{
123
128
DotNetNuke . Framework . JavaScriptLibraries . JavaScript . RequestRegistration ( CommonJs . DnnPlugins ) ;
124
129
var portalId = ModuleContext . PortalId ;
125
- var user = UserController . GetUserByName ( portalId , item . Username ) ;
126
130
ModelState . Remove ( "UserId" ) ;
127
- if ( user != null )
131
+ if ( ! ModelState . IsValid )
128
132
{
129
- string errorMessage = Localization . GetString ( "UsernameInUse.Text" , ResourceFile ) ;
130
- ModelState . AddModelError ( string . Empty , @errorMessage ) ;
131
133
return View ( item ) ;
132
134
}
133
-
134
- if ( ! ModelState . IsValid )
135
+ var userCreateStatus = UserRepository . CreateUser ( item , portalId ) ;
136
+ if ( userCreateStatus == UserCreateStatus . Success )
137
+ {
138
+ return RedirectToAction ( "Index" ) ;
139
+ }
140
+ else
135
141
{
142
+ switch ( userCreateStatus )
143
+ {
144
+ case UserCreateStatus . DuplicateEmail :
145
+ ModelState . AddModelError ( "Email" , "Duplicate; Email Address already in use on another User Account" ) ;
146
+ break ;
147
+ case UserCreateStatus . InvalidEmail :
148
+ ModelState . AddModelError ( "Email" , "Invalid; Email Address did not pass validation" ) ;
149
+ break ;
150
+ case UserCreateStatus . InvalidPassword :
151
+ ModelState . AddModelError ( "Password" , "Invalid; Password requirements were not met" ) ;
152
+ break ;
153
+ case UserCreateStatus . BannedPasswordUsed :
154
+ ModelState . AddModelError ( "Password" , "Invalid; Password is banned" ) ;
155
+ break ;
156
+ case UserCreateStatus . DuplicateUserName :
157
+ case UserCreateStatus . UserAlreadyRegistered :
158
+ case UserCreateStatus . UsernameAlreadyExists :
159
+ ModelState . AddModelError ( "Username" , "Duplicate; Username already in use on another User Account" ) ;
160
+ break ;
161
+ case UserCreateStatus . InvalidUserName :
162
+ ModelState . AddModelError ( "Username" , "Invalid; Username does not meet requirements" ) ;
163
+ break ;
164
+ default :
165
+ ModelState . AddModelError ( string . Empty , $ "Create User Failed. Unhandled or Unknown UserCreateStatus: { userCreateStatus } ") ;
166
+ break ;
167
+ }
168
+ string errorMessage = UserController . GetUserCreateStatus ( userCreateStatus ) ;
169
+ ModelState . AddModelError ( string . Empty , errorMessage ) ;
136
170
return View ( item ) ;
137
171
}
138
- UserRepository . CreateUser ( item , portalId ) ;
139
- return RedirectToAction ( "Index" ) ;
140
172
}
141
173
142
174
public ActionResult Edit ( int itemId )
@@ -249,23 +281,111 @@ public ActionResult UserRoles(double? take, int? pageIndex, int? goToPage, strin
249
281
var result1 = UserRepository . GetRolesByUser ( takeValue , pageIndexValue , goToPage , portalId , search , itemId ) ;
250
282
return View ( result1 ) ;
251
283
}
252
-
253
- if ( roleId != null )
254
- {
255
- if ( actionView == "Add" )
256
- {
257
- RoleController . Instance . AddUserRole ( portalId , itemId , roleIdValue , RoleStatus . Approved , false , DateTime . Now , DateTime . Now . AddDays ( 30 ) ) ;
258
- }
259
- else
260
- {
261
- RoleController . Instance . UpdateUserRole ( portalId , itemId , roleIdValue , RoleStatus . Approved , false , true ) ;
262
- }
263
- }
264
284
ViewBag . User = UserRepository . GetUser ( portalId , itemId ) ;
265
285
var result = UserRepository . GetRolesByUser ( takeValue , pageIndexValue , goToPage , portalId , search , itemId ) ;
266
286
return View ( result ) ;
267
287
}
268
288
}
289
+
290
+ [ HttpPost ]
291
+ public ActionResult AddUserRole ( int itemId , int roleId )
292
+ {
293
+ bool isAuthenticated = Request . IsAuthenticated ;
294
+ // Check if the authenticated user has the required permission
295
+ var hasPermission = Functions . HasPermission ( ModuleContext ) ;
296
+
297
+ if ( ! isAuthenticated || ! hasPermission )
298
+ {
299
+ string errorMessage = Localization . GetString ( "NotPermissions.Text" , ResourceFile ) ;
300
+ ViewBag . ErrorMessage = errorMessage ;
301
+ return View ( "Error" ) ;
302
+ }
303
+ else
304
+ {
305
+ try
306
+ {
307
+ var portalId = ModuleContext . PortalId ;
308
+ RoleController . Instance . AddUserRole ( portalId , itemId , roleId , RoleStatus . Approved , false , DateTime . MinValue , DateTime . MinValue ) ;
309
+ }
310
+ catch ( Exception ex )
311
+ {
312
+ // Log the exception
313
+ LoggerSource . Instance . GetLogger ( typeof ( UserRepository ) ) . Error ( ex ) ;
314
+ }
315
+ return Content ( "" ) ;
316
+ }
317
+ }
318
+
319
+ [ HttpPost ]
320
+ public ActionResult RemoveUserRole ( int itemId , int roleId )
321
+ {
322
+ bool isAuthenticated = Request . IsAuthenticated ;
323
+ // Check if the authenticated user has the required permission
324
+ var hasPermission = Functions . HasPermission ( ModuleContext ) ;
325
+
326
+ if ( ! isAuthenticated || ! hasPermission )
327
+ {
328
+ string errorMessage = Localization . GetString ( "NotPermissions.Text" , ResourceFile ) ;
329
+ ViewBag . ErrorMessage = errorMessage ;
330
+ return View ( "Error" ) ;
331
+ }
332
+ else
333
+ {
334
+ try
335
+ {
336
+ var portalId = ModuleContext . PortalId ;
337
+ RoleController . Instance . UpdateUserRole ( portalId , itemId , roleId , RoleStatus . Approved , false , true ) ;
338
+ }
339
+ catch ( Exception ex )
340
+ {
341
+ // Log the exception
342
+ LoggerSource . Instance . GetLogger ( typeof ( UserRepository ) ) . Error ( ex ) ;
343
+ }
344
+ return Content ( "" ) ;
345
+ }
346
+ }
347
+
348
+ [ HttpPost ]
349
+ public ActionResult UpdateDateTimeUserRole ( int itemId , int roleId , DateTime ? effectiveDate , DateTime ? expiryDate )
350
+ {
351
+ try
352
+ {
353
+ var portalId = ModuleContext . PortalId ;
354
+ UserRepository . UpdateDateTimeUserRole ( portalId , itemId , roleId , effectiveDate , expiryDate ) ;
355
+ }
356
+ catch ( Exception ex )
357
+ {
358
+ // Log the exception
359
+ LoggerSource . Instance . GetLogger ( typeof ( UserRepository ) ) . Error ( ex ) ;
360
+ }
361
+ return Content ( "" ) ;
362
+ }
363
+
364
+ public ActionResult SetDateTimeUserRoleNull ( int itemId , int roleId )
365
+ {
366
+ try
367
+ {
368
+ var portalId = ModuleContext . PortalId ;
369
+ var roleController = new RoleController ( ) ;
370
+ roleController . AddUserRole ( portalId , itemId , roleId , DateTime . MinValue , DateTime . MinValue ) ;
371
+
372
+ // Log the action
373
+ var user = UserController . GetUserById ( portalId , itemId ) ;
374
+ UserRoleInfo userRole = roleController . GetUserRole ( portalId , itemId , roleId ) ;
375
+ var currentUser = UserController . Instance . GetCurrentUserInfo ( ) ;
376
+
377
+ var logMessage = $ "The effective date and expiration date for Role { userRole . FullName } were cleared for User { user . Username } by Username { currentUser . Username } .";
378
+ var logger = LoggerSource . Instance . GetLogger ( typeof ( UserManageController ) ) ;
379
+ logger . Info ( logMessage ) ;
380
+ }
381
+ catch ( Exception ex )
382
+ {
383
+ // Log the exception
384
+ LoggerSource . Instance . GetLogger ( typeof ( UserManageController ) ) . Error ( ex ) ;
385
+ }
386
+ return Content ( "" ) ;
387
+ }
388
+
269
389
public ActionResult PasswordResetLink ( int itemId )
270
390
{
271
391
var portalId = ModuleContext . PortalId ;
0 commit comments