)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Index2 (Ia.Ftn.Wa.Models.Application)

Public general use code classes and xml files that we've compiled and used over the years:

    1: using Microsoft.AspNetCore.Identity;
    2: using Microsoft.AspNetCore.Mvc.Rendering;
    3: using System.Net;
    4:  
    5: namespace Ia.Ftn.Wa.Models.Application
    6: {
    7:     ////////////////////////////////////////////////////////////////////////////
    8:  
    9:     /// <summary publish="true">
   10:     ///
   11:     /// </summary>
   12:     /// 
   13:     /// <remarks> 
   14:     /// Copyright © 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   15:     ///
   16:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   17:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   18:     ///
   19:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   20:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   21:     /// 
   22:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   23:     /// 
   24:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   25:     /// </remarks> 
   26:     public static class Index2
   27:     {
   28:         private static int selectLevel;
   29:  
   30:         ////////////////////////////////////////////////////////////////////////////
   31:  
   32:         /// <summary>
   33:         ///
   34:         /// </summary>
   35:         public static void UserRoleViewModel(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, RoleManager<IdentityRole> roleManager, ref IdentityViewModel currentIdentityViewModel, ref IdentityViewModel identityViewModel)
   36:         {
   37:             UserSelectList_ViewModel(ref identityViewModel);
   38:             RoleSelectList_ViewModel(ref identityViewModel, roleManager);
   39:  
   40:             UserRoleSelectList_ViewModel(ref identityViewModel, userManager);
   41:  
   42:             var roleName = identityViewModel.RoleSelectedValue;
   43:  
   44:             if (!string.IsNullOrEmpty(roleName))
   45:             {
   46:                 identityViewModel.RoleUserListLabel = "Users in role: ";
   47:  
   48:                 var usersInRoleList = Ia.Ftn.Cl.Models.Identity.UsersInRoleList(userManager, roleName);
   49:  
   50:                 foreach (var user in usersInRoleList)
   51:                 {
   52:                     identityViewModel.RoleUserListLabel += user.FirstAndMiddleNameBracketUserName + ", ";
   53:                 }
   54:             }
   55:             else
   56:             {
   57:                 identityViewModel.RoleUserListLabel = string.Empty;
   58:             }
   59:         }
   60:  
   61:  
   62:         ////////////////////////////////////////////////////////////////////////////
   63:  
   64:         /// <summary>
   65:         ///
   66:         /// </summary>
   67:         private static void UserSelectList_ViewModel(ref IdentityViewModel identityViewModel)
   68:         {
   69:             var list = Ia.Ftn.Cl.Models.Identity.StaffIdentityUserList();
   70:  
   71:             identityViewModel.UserSelectList = new SelectList(list, "UserName", "FirstAndMiddleNameBracketUserName");
   72:         }
   73:  
   74:         ////////////////////////////////////////////////////////////////////////////
   75:  
   76:         /// <summary>
   77:         ///
   78:         /// </summary>
   79:         private static void RoleSelectList_ViewModel(ref IdentityViewModel identityViewModel, RoleManager<IdentityRole> roleManager)
   80:         {
   81:             var list = Ia.Ftn.Cl.Models.Identity.RoleList(roleManager);
   82:  
   83:             identityViewModel.RoleSelectList = new SelectList(list, "Name", "Name");
   84:  
   85:             if (identityViewModel.RoleSelectList.Count() == 0)
   86:             {
   87:                 //roleDropDownList.Enabled = false;
   88:                 identityViewModel.AddButtonEnabled = false;
   89:             }
   90:             else
   91:             {
   92:                 //roleDropDownList.Enabled = true;
   93:                 identityViewModel.AddButtonEnabled = true;
   94:             }
   95:         }
   96:  
   97:         ////////////////////////////////////////////////////////////////////////////
   98:  
   99:         /// <summary>
  100:         ///
  101:         /// </summary>
  102:         private static void UserRoleSelectList_ViewModel(ref IdentityViewModel identityViewModel, UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager)
  103:         {
  104:             var userName = identityViewModel.UserSelectedValue;
  105:  
  106:             if (!string.IsNullOrEmpty(userName))
  107:             {
  108:                 var list = Ia.Ftn.Cl.Models.Identity.RolesForUserList(userManager, userName).ToList();
  109:  
  110:                 identityViewModel.UserRoleSelectList = new SelectList(list);
  111:  
  112:                 if (identityViewModel.UserRoleSelectList.Count() == 0)
  113:                 {
  114:                     //userRoleDropDownList.Enabled = false;
  115:                     identityViewModel.DeleteButtonEnabled = false;
  116:                 }
  117:                 else
  118:                 {
  119:                     //userRoleDropDownList.Enabled = true;
  120:                     identityViewModel.DeleteButtonEnabled = true;
  121:                 }
  122:             }
  123:             else
  124:             {
  125:                 //userRoleDropDownList.Enabled = true;
  126:                 identityViewModel.DeleteButtonEnabled = true;
  127:             }
  128:         }
  129:  
  130:         ////////////////////////////////////////////////////////////////////////////
  131:  
  132:         /// <summary>
  133:         ///
  134:         /// </summary>
  135:         public static void AddUserToRole(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, RoleManager<IdentityRole> roleManager, ref IdentityViewModel identityViewModel)
  136:         {
  137:             var userName = identityViewModel.UserSelectedValue;
  138:             var roleName = identityViewModel.RoleSelectedValue;
  139:  
  140:             identityViewModel.Result = new Ia.Cl.Models.Result();
  141:  
  142:             if (!Ia.Ftn.Cl.Models.Identity.IsUserInRole(userManager, userName, roleName))
  143:             {
  144:                 var identityResult = Ia.Ftn.Cl.Models.Identity.AddUserToRole(userManager, userName, roleName);
  145:  
  146:                 if (identityResult.Succeeded)
  147:                 {
  148:                     identityViewModel.Result.AddSuccess("User added to role");
  149:                 }
  150:                 else
  151:                 {
  152:                     identityViewModel.Result.AddError("Could not add the user to the role: " + identityResult.Errors.First());
  153:                 }
  154:             }
  155:             else
  156:             {
  157:                 identityViewModel.Result.AddError("User is not in role");
  158:             }
  159:         }
  160:  
  161:         ////////////////////////////////////////////////////////////////////////////
  162:  
  163:         /// <summary>
  164:         ///
  165:         /// </summary>
  166:         public static void DeleteUserFromRole(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, RoleManager<IdentityRole> roleManager, ref IdentityViewModel identityViewModel)
  167:         {
  168:             var userName = identityViewModel.UserSelectedValue;
  169:             var roleName = identityViewModel.UserRoleSelectedValue;
  170:  
  171:             identityViewModel.Result = new Ia.Cl.Models.Result();
  172:  
  173:             if (Ia.Ftn.Cl.Models.Identity.IsUserInRole(userManager, userName, roleName))
  174:             {
  175:                 var identityResult = Ia.Ftn.Cl.Models.Identity.RemoveUserFromRole(userManager, roleManager, userName, roleName);
  176:  
  177:                 if (identityResult.Succeeded)
  178:                 {
  179:                     identityViewModel.Result.AddSuccess("User removed from role");
  180:                 }
  181:                 else
  182:                 {
  183:                     identityViewModel.Result.AddError("Could not remove user from role: " + identityResult.Errors.First());
  184:                 }
  185:             }
  186:             else
  187:             {
  188:                 identityViewModel.Result.AddError("User is already in role");
  189:             }
  190:         }
  191:  
  192:         ///////////////////////////////////////////////////////////////////////////////
  193:         ////////////////////////////////////////////////////////////////////////////
  194:  
  195:  
  196:  
  197:  
  198:  
  199:  
  200:  
  201:  
  202:  
  203:  
  204:  
  205:  
  206:  
  207:  
  208:  
  209:         ///////////////////////////////////////////////////////////////////////////////
  210:         ////////////////////////////////////////////////////////////////////////////
  211:  
  212:         /// <summary>
  213:         ///
  214:         /// </summary>
  215:         public static void IdentityPost(ref Ia.Ftn.Wa.Models.Application.IdentityViewModel identityViewModel, string commandName, string userId)
  216:         {
  217:             if (commandName == "insert") Insert(ref identityViewModel);
  218:             else if (commandName == "edit") Edit(ref identityViewModel, userId);
  219:             else if (commandName == "update") Update(ref identityViewModel, userId);
  220:             else if (commandName == "delete") Delete(ref identityViewModel, userId);
  221:             else if (commandName == "cancel") Cancel(ref identityViewModel);
  222:             else
  223:             {
  224:             }
  225:         }
  226:  
  227:         ////////////////////////////////////////////////////////////////////////////
  228:  
  229:         /// <summary>
  230:         ///
  231:         /// </summary>
  232:         private static void Insert(ref Ia.Ftn.Wa.Models.Application.IdentityViewModel identityViewModel)
  233:         {
  234:             Ia.Ftn.Wa.Models.Application.Index2.CreateUser(identityViewModel.UserManager, identityViewModel.RoleManager, ref identityViewModel);
  235:         }
  236:  
  237:         ////////////////////////////////////////////////////////////////////////////
  238:  
  239:         /// <summary>
  240:         ///
  241:         /// </summary>
  242:         private static void Edit(ref Ia.Ftn.Wa.Models.Application.IdentityViewModel identityViewModel, string userId)
  243:         {
  244:             identityViewModel.StaffIdentityUserListEditId = userId;
  245:         }
  246:  
  247:         ////////////////////////////////////////////////////////////////////////////
  248:  
  249:         /// <summary>
  250:         ///
  251:         /// </summary>
  252:         private static void Cancel(ref Ia.Ftn.Wa.Models.Application.IdentityViewModel identityViewModel)
  253:         {
  254:             identityViewModel.StaffIdentityUserListEditId = string.Empty;
  255:         }
  256:  
  257:         ////////////////////////////////////////////////////////////////////////////
  258:  
  259:         /// <summary>
  260:         /// 
  261:         /// </summary>
  262:         private static void Update(ref Ia.Ftn.Wa.Models.Application.IdentityViewModel identityViewModel, string userId)
  263:         {
  264:             Ia.Cl.Models.Result result;
  265:             DateTime now;
  266:  
  267:             identityViewModel.UpdateResult = new Ia.Cl.Models.Result();
  268:  
  269:             identityViewModel.StaffIdentityUserListEditId = userId;
  270:  
  271:             if (Ia.Ftn.Cl.Models.Business.Authority.StaffCanCreateReadUpdateDeleteUsers(identityViewModel.CurrentStaffIdentityUser))
  272:             {
  273:                 now = DateTime.UtcNow.AddHours(3);
  274:  
  275:                 // note = WebUtility.HtmlDecode(identityViewModel.NoteTextAreaEdit);
  276:  
  277:                 var staffIdentityUser = new Ia.Ftn.Cl.Models.StaffIdentityUser()
  278:                 {
  279:                     Id = userId,
  280:                     Updated = now
  281:                 };
  282:  
  283:                 //Ia.Ftn.Wa.Models.Application.Index2.UpdateUser(ref identityViewModel, userId);
  284:  
  285:                 var isSuccess = false;
  286:  
  287:                 if (isSuccess)
  288:                 {
  289:                     identityViewModel.UpdateResult.AddSuccess("User updated (تم تحديث المستخدم)");
  290:  
  291:                     identityViewModel.StaffIdentityUserListEditId = string.Empty;
  292:                 }
  293:                 else identityViewModel.UpdateResult.AddError(""); // result.MessageWithoutCaption);
  294:             }
  295:             else identityViewModel.UpdateResult.AddError("You are not authorized to modify this value (لست مخولاً بتعديل هذه القيمة)");
  296:         }
  297:  
  298:         ////////////////////////////////////////////////////////////////////////////
  299:  
  300:         /// <summary>
  301:         /// 
  302:         /// </summary>
  303:         private static void Delete(ref Ia.Ftn.Wa.Models.Application.IdentityViewModel identityViewModel, string userId)
  304:         {
  305:             Ia.Ftn.Wa.Models.Application.Index2.DeleteUser(identityViewModel.UserManager, ref identityViewModel, userId);
  306:         }
  307:  
  308:         ////////////////////////////////////////////////////////////////////////////
  309:         ////////////////////////////////////////////////////////////////////////////
  310:  
  311:  
  312:  
  313:  
  314:  
  315:  
  316:  
  317:  
  318:  
  319:  
  320:  
  321:  
  322:  
  323:  
  324:  
  325:  
  326:  
  327:  
  328:  
  329:  
  330:  
  331:  
  332:  
  333:         ////////////////////////////////////////////////////////////////////////////
  334:         ////////////////////////////////////////////////////////////////////////////
  335:  
  336:         /// <summary>
  337:         ///
  338:         /// </summary>
  339:         public static void RegisterViewModel(ref IdentityViewModel currentIdentityViewModel, ref IdentityViewModel identityViewModel)
  340:         {
  341:             CompareCurrentAndNewViewModels(ref currentIdentityViewModel, ref identityViewModel);
  342:  
  343:             var identityUsers = Ia.Ftn.Cl.Models.Identity.StaffIdentityUserList();
  344:  
  345:             identityViewModel.StaffIdentityUserList = identityUsers;
  346:  
  347:             var list = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.Type == "ministry" || f.Type == "supplier" select f).ToList();
  348:  
  349:             if (!string.IsNullOrEmpty(identityViewModel.Framework1SelectedValue))
  350:             {
  351:                 identityViewModel.Framework1SelectList = new SelectList(list, "Id", "ArabicName", identityViewModel.Framework1SelectedValue);
  352:             }
  353:             else
  354:             {
  355:                 identityViewModel.Framework1SelectList = new SelectList(list, "Id", "ArabicName", list[0].Id.ToString());
  356:                 identityViewModel.Framework1SelectedValue = list[0].Id.ToString();
  357:             }
  358:  
  359:             Framework1SelectList_SelectedIndexChanged(ref identityViewModel);
  360:  
  361:             currentIdentityViewModel.Framework1SelectedValue = identityViewModel.Framework1SelectedValue;
  362:             currentIdentityViewModel.Framework2SelectedValue = identityViewModel.Framework2SelectedValue;
  363:             currentIdentityViewModel.Framework3SelectedValue = identityViewModel.Framework3SelectedValue;
  364:             currentIdentityViewModel.Framework4SelectedValue = identityViewModel.Framework4SelectedValue;
  365:             currentIdentityViewModel.Framework5SelectedValue = identityViewModel.Framework5SelectedValue;
  366:             currentIdentityViewModel.Framework6SelectedValue = identityViewModel.Framework6SelectedValue;
  367:         }
  368:  
  369:         ////////////////////////////////////////////////////////////////////////////
  370:  
  371:         /// <summary>
  372:         ///
  373:         /// </summary>
  374:         public static void Framework1SelectList_SelectedIndexChanged(ref IdentityViewModel identityViewModel)
  375:         {
  376:             if (identityViewModel.Framework1SelectList.Count() > 0)
  377:             {
  378:                 if (!string.IsNullOrEmpty(identityViewModel.Framework1SelectedValue))
  379:                 {
  380:                     selectLevel = 1;
  381:  
  382:                     var id = identityViewModel.Framework1SelectedValue;
  383:  
  384:                     var parent = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList
  385:                                   where f.Id == id
  386:                                   select f).SingleOrDefault();
  387:  
  388:                     if (parent != null)
  389:                     {
  390:                         var list = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList
  391:                                     where f.Parent != null && f.Parent.Id == parent.Id
  392:                                     select f).ToList();
  393:  
  394:                         if (parent != null && parent.Type == "ministry")
  395:                         {
  396:                             list.Add(new Ia.Ftn.Cl.Models.Business.Administration.Framework { Id = id + "-0", ArabicName = "وزير" });
  397:                         }
  398:                         else if (parent != null && parent.Type == "supplier")
  399:                         {
  400:                             list.Add(new Ia.Ftn.Cl.Models.Business.Administration.Framework { Id = id + "-0", ArabicName = "مدير تنفيذي" });
  401:                             list.Add(new Ia.Ftn.Cl.Models.Business.Administration.Framework { Id = id + "-1", ArabicName = "موظف" });
  402:                         }
  403:  
  404:                         if (list.Count > 0)
  405:                         {
  406:                             if (!string.IsNullOrEmpty(identityViewModel.Framework2SelectedValue))
  407:                             {
  408:                                 identityViewModel.Framework2SelectList = new SelectList(list, "Id", "ArabicName", identityViewModel.Framework2SelectedValue);
  409:                             }
  410:                             else
  411:                             {
  412:                                 identityViewModel.Framework2SelectList = new SelectList(list, "Id", "ArabicName", list[0].Id.ToString());
  413:                                 identityViewModel.Framework2SelectedValue = list[0].Id.ToString();
  414:                             }
  415:  
  416:                             Framework2SelectList_SelectedIndexChanged(ref identityViewModel);
  417:                         }
  418:                     }
  419:                 }
  420:             }
  421:         }
  422:  
  423:         ////////////////////////////////////////////////////////////////////////////
  424:  
  425:         /// <summary>
  426:         ///
  427:         /// </summary>
  428:         public static void Framework2SelectList_SelectedIndexChanged(ref IdentityViewModel identityViewModel)
  429:         {
  430:             if (identityViewModel.Framework2SelectList.Count() > 0)
  431:             {
  432:                 if (!string.IsNullOrEmpty(identityViewModel.Framework2SelectedValue))
  433:                 {
  434:                     selectLevel = 2;
  435:  
  436:                     var id = identityViewModel.Framework2SelectedValue;
  437:  
  438:                     if (!id.Contains("-0") && !id.Contains("-1"))
  439:                     {
  440:                         var list = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.Parent != null && f.Parent.Id == id select f).ToList();
  441:                         list.Add(new Ia.Ftn.Cl.Models.Business.Administration.Framework { Id = id + "-0", ArabicName = "وكيل" });
  442:  
  443:                         if (list.Count > 0)
  444:                         {
  445:                             if (!string.IsNullOrEmpty(identityViewModel.Framework3SelectedValue))
  446:                             {
  447:                                 identityViewModel.Framework3SelectList = new SelectList(list, "Id", "ArabicName", identityViewModel.Framework3SelectedValue);
  448:                             }
  449:                             else
  450:                             {
  451:                                 identityViewModel.Framework3SelectList = new SelectList(list, "Id", "ArabicName", list[0].Id.ToString());
  452:                                 identityViewModel.Framework3SelectedValue = list[0].Id.ToString();
  453:                             }
  454:  
  455:                             Framework3SelectList_SelectedIndexChanged(ref identityViewModel);
  456:                         }
  457:                     }
  458:                 }
  459:             }
  460:         }
  461:  
  462:         ////////////////////////////////////////////////////////////////////////////
  463:  
  464:         /// <summary>
  465:         ///
  466:         /// </summary>
  467:         public static void Framework3SelectList_SelectedIndexChanged(ref IdentityViewModel identityViewModel)
  468:         {
  469:             if (identityViewModel.Framework3SelectList.Count() > 0)
  470:             {
  471:                 if (!string.IsNullOrEmpty(identityViewModel.Framework3SelectedValue))
  472:                 {
  473:                     selectLevel = 3;
  474:  
  475:                     var id = identityViewModel.Framework3SelectedValue;
  476:  
  477:                     if (!id.Contains("-0") && !id.Contains("-1"))
  478:                     {
  479:                         var list = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.Parent != null && f.Parent.Id == id select f).ToList();
  480:                         list.Add(new Ia.Ftn.Cl.Models.Business.Administration.Framework { Id = id + "-0", ArabicName = "مدير" });
  481:  
  482:                         if (list.Count > 0)
  483:                         {
  484:                             if (!string.IsNullOrEmpty(identityViewModel.Framework4SelectedValue))
  485:                             {
  486:                                 identityViewModel.Framework4SelectList = new SelectList(list, "Id", "ArabicName", identityViewModel.Framework4SelectedValue);
  487:                             }
  488:                             else
  489:                             {
  490:                                 identityViewModel.Framework4SelectList = new SelectList(list, "Id", "ArabicName", list[0].Id.ToString());
  491:                                 identityViewModel.Framework4SelectedValue = list[0].Id.ToString();
  492:                             }
  493:  
  494:                             Framework4SelectList_SelectedIndexChanged(ref identityViewModel);
  495:                         }
  496:                     }
  497:                 }
  498:             }
  499:         }
  500:  
  501:         ////////////////////////////////////////////////////////////////////////////
  502:  
  503:         /// <summary>
  504:         ///
  505:         /// </summary>
  506:         public static void Framework4SelectList_SelectedIndexChanged(ref IdentityViewModel identityViewModel)
  507:         {
  508:             if (identityViewModel.Framework4SelectList.Count() > 0)
  509:             {
  510:                 if (!string.IsNullOrEmpty(identityViewModel.Framework4SelectedValue))
  511:                 {
  512:                     selectLevel = 4;
  513:  
  514:                     var id = identityViewModel.Framework4SelectedValue;
  515:  
  516:                     if (!id.Contains("-0") && !id.Contains("-1"))
  517:                     {
  518:                         var list = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.Parent != null && f.Parent.Id == id select f).ToList();
  519:                         list.Add(new Ia.Ftn.Cl.Models.Business.Administration.Framework { Id = id + "-0", ArabicName = "مراقب" });
  520:  
  521:                         if (list.Count > 0)
  522:                         {
  523:                             if (!string.IsNullOrEmpty(identityViewModel.Framework5SelectedValue))
  524:                             {
  525:                                 identityViewModel.Framework5SelectList = new SelectList(list, "Id", "ArabicName", identityViewModel.Framework5SelectedValue);
  526:                             }
  527:                             else
  528:                             {
  529:                                 identityViewModel.Framework5SelectList = new SelectList(list, "Id", "ArabicName", list[0].Id.ToString());
  530:                                 identityViewModel.Framework5SelectedValue = list[0].Id.ToString();
  531:                             }
  532:  
  533:                             Framework5SelectList_SelectedIndexChanged(ref identityViewModel);
  534:                         }
  535:                     }
  536:                 }
  537:             }
  538:         }
  539:  
  540:         ////////////////////////////////////////////////////////////////////////////
  541:  
  542:         /// <summary>
  543:         ///
  544:         /// </summary>
  545:         public static void Framework5SelectList_SelectedIndexChanged(ref IdentityViewModel identityViewModel)
  546:         {
  547:             if (identityViewModel.Framework5SelectList.Count() > 0)
  548:             {
  549:                 if (!string.IsNullOrEmpty(identityViewModel.Framework5SelectedValue))
  550:                 {
  551:                     selectLevel = 5;
  552:  
  553:                     var id = identityViewModel.Framework5SelectedValue;
  554:  
  555:                     if (!id.Contains("-0") && !id.Contains("-1"))
  556:                     {
  557:                         var list = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.Parent != null && f.Parent.Id == id select f).ToList();
  558:                         list.Add(new Ia.Ftn.Cl.Models.Business.Administration.Framework { Id = id + "-0", ArabicName = "رئيس قسم" });
  559:                         list.Add(new Ia.Ftn.Cl.Models.Business.Administration.Framework { Id = id + "-1", ArabicName = "موظف" });
  560:  
  561:                         if (list.Count > 0)
  562:                         {
  563:                             if (!string.IsNullOrEmpty(identityViewModel.Framework6SelectedValue))
  564:                             {
  565:                                 identityViewModel.Framework6SelectList = new SelectList(list, "Id", "ArabicName", identityViewModel.Framework6SelectedValue);
  566:                             }
  567:                             else
  568:                             {
  569:                                 identityViewModel.Framework6SelectList = new SelectList(list, "Id", "ArabicName", list[0].Id.ToString());
  570:                                 identityViewModel.Framework6SelectedValue = list[0].Id.ToString();
  571:                             }
  572:  
  573:                             //Framework6SelectList_SelectedIndexChanged(ref identityViewModel);
  574:                         }
  575:                     }
  576:                 }
  577:             }
  578:         }
  579:  
  580:         ////////////////////////////////////////////////////////////////////////////
  581:  
  582:         /// <summary>
  583:         ///
  584:         /// </summary>
  585:         public static void CreateUser(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, RoleManager<IdentityRole> roleManager, ref IdentityViewModel identityViewModel)
  586:         {
  587:             bool isHead;
  588:             string frameworkId;
  589:  
  590:             var firstName = WebUtility.HtmlDecode(identityViewModel.CreateUserFirstNameText);
  591:             var middleName = WebUtility.HtmlDecode(identityViewModel.CreateUserMiddleNameText);
  592:             var lastName = WebUtility.HtmlDecode(identityViewModel.CreateUserLastNameText);
  593:  
  594:             switch (selectLevel)
  595:             {
  596:                 case 1:
  597:                     {
  598:                         isHead = identityViewModel.Framework1SelectedValue.Contains("-0") == true;
  599:                         frameworkId = identityViewModel.Framework1SelectedValue.Replace("-0", "");
  600:                         break;
  601:                     }
  602:                 case 2:
  603:                     {
  604:                         isHead = identityViewModel.Framework2SelectedValue.Contains("-0") == true;
  605:                         frameworkId = identityViewModel.Framework2SelectedValue.Replace("-0", "");
  606:                         break;
  607:                     }
  608:                 case 3:
  609:                     {
  610:                         isHead = identityViewModel.Framework3SelectedValue.Contains("-0") == true;
  611:                         frameworkId = identityViewModel.Framework3SelectedValue.Replace("-0", "");
  612:                         break;
  613:                     }
  614:                 case 4:
  615:                     {
  616:                         isHead = identityViewModel.Framework4SelectedValue.Contains("-0") == true;
  617:                         frameworkId = identityViewModel.Framework4SelectedValue.Replace("-0", "");
  618:                         break;
  619:                     }
  620:                 case 5:
  621:                     {
  622:                         isHead = identityViewModel.Framework5SelectedValue.Contains("-0") == true;
  623:                         frameworkId = identityViewModel.Framework5SelectedValue.Replace("-0", "");
  624:  
  625:                         if (!isHead)
  626:                         {
  627:                             isHead = identityViewModel.Framework6SelectedValue.Contains("-0") == true;
  628:                         }
  629:  
  630:                         break;
  631:                     }
  632:  
  633:                 default: isHead = false; frameworkId = string.Empty; break;
  634:             }
  635:  
  636:  
  637:             var email = WebUtility.HtmlDecode(identityViewModel.CreateUserEmailText);
  638:  
  639:             var userName = WebUtility.HtmlDecode(identityViewModel.CreateUserUserNameText);
  640:             var phoneNumber = WebUtility.HtmlDecode(identityViewModel.CreateUserPhoneNumberText);
  641:             var password = WebUtility.HtmlDecode(identityViewModel.CreateUserPasswordText);
  642:             var confirmPassword = WebUtility.HtmlDecode(identityViewModel.CreateUserConfirmPasswordText);
  643:  
  644:             identityViewModel.Result = new Ia.Cl.Models.Result();
  645:  
  646:             if (!string.IsNullOrEmpty(firstName))
  647:             {
  648:                 if (!string.IsNullOrEmpty(lastName))
  649:                 {
  650:                     if (!string.IsNullOrEmpty(frameworkId))
  651:                     {
  652:                         var framework = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList
  653:                                          where f.Id == frameworkId
  654:                                          select f).Single();
  655:                         var hasAuthority = !Ia.Ftn.Cl.Models.Business.Authority.FrameworkIsApplication(framework)
  656:                             || Ia.Ftn.Cl.Models.Business.Authority.FrameworkIsApplication(framework) && Ia.Ftn.Cl.Models.Business.Authority.StaffIsApplication(identityViewModel.CurrentStaffIdentityUser);
  657:  
  658:                         if (hasAuthority)
  659:                         {
  660:                             if (!isHead || isHead && !Ia.Ftn.Cl.Models.Business.Administration.FrameworkHasAHead(framework))
  661:                             {
  662:                                 if (!string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(confirmPassword))
  663:                                 {
  664:                                     if (password == confirmPassword)
  665:                                     {
  666:                                         var staffIdentityUser = Ia.Ftn.Cl.Models.Identity.CreateUser(userManager, firstName, middleName, lastName, frameworkId, isHead, email, userName, phoneNumber, password, out IdentityResult identityResult);
  667:  
  668:                                         if (identityResult.Succeeded)
  669:                                         {
  670:                                             Ia.Ftn.Cl.Models.Business.Authority.UpdateFrameworkRolesOfStaffIdentityUser(userManager, roleManager, staffIdentityUser);
  671:  
  672:                                             identityViewModel.Result.AddSuccess("User created.");
  673:                                         }
  674:                                         else
  675:                                         {
  676:                                             identityViewModel.Result.AddError("User not created: " + identityResult.Errors.First().Description);
  677:                                         }
  678:                                     }
  679:                                     else
  680:                                     {
  681:                                         identityViewModel.Result.AddError("Password and its verification do not match.");
  682:                                     }
  683:                                 }
  684:                                 else
  685:                                 {
  686:                                     identityViewModel.Result.AddError("Password is null or empty.");
  687:                                 }
  688:                             }
  689:                             else
  690:                             {
  691:                                 identityViewModel.Result.AddError("Can't create staff as head because framework already has a head.");
  692:                             }
  693:                         }
  694:                         else
  695:                         {
  696:                             identityViewModel.Result.AddError("Can't create staff application.");
  697:                         }
  698:                     }
  699:                     else
  700:                     {
  701:                         identityViewModel.Result.AddError("frameworkId is null or empty.");
  702:                     }
  703:                 }
  704:                 else
  705:                 {
  706:                     identityViewModel.Result.AddError("Last name is null or empty.");
  707:                 }
  708:             }
  709:             else
  710:             {
  711:                 identityViewModel.Result.AddError("First name is null or empty.");
  712:             }
  713:         }
  714:  
  715:         /*
  716:         ////////////////////////////////////////////////////////////////////////////
  717: 
  718:         /// <summary>
  719:         ///
  720:         /// </summary>
  721:         protected void UserGridView_RowEditing(object sender, GridViewEditEventArgs e)
  722:         {
  723:             userGridView.EditIndex = e.NewEditIndex;
  724:         }
  725: 
  726:         ////////////////////////////////////////////////////////////////////////////
  727: 
  728:         /// <summary>
  729:         ///
  730:         /// </summary>
  731:         protected void UserGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  732:         {
  733:             userGridView.EditIndex = -1;
  734:         }
  735: 
  736:         ////////////////////////////////////////////////////////////////////////////
  737: 
  738:         /// <summary>
  739:         ///
  740:         /// </summary>
  741:         protected void UserGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
  742:         {
  743:             bool isSuccess;
  744:             string email, userName;
  745:             Guid providerUserKey;
  746:             DateTime now;
  747:             GridViewRow gridViewRow;
  748:             MembershipUser membershipUser;
  749: 
  750:             isSuccess = false;
  751:             userGridView.EditIndex = -1;
  752:             gridViewRow = userGridView.Rows[e.RowIndex];
  753: 
  754:             if (gridViewRow != null && userGridView.Rows.Count > 0)
  755:             {
  756:                 try
  757:                 {
  758:                     now = DateTime.UtcNow.AddHours(3);
  759: 
  760:                     providerUserKey = Guid.Parse(userGridView.DataKeys[e.RowIndex].Values["ProviderUserKey"].ToString());
  761:                     email = Server.HtmlDecode(((TextBox)gridViewRow.FindControl("userEmailTextBox")).Text);
  762:                     userName = Server.HtmlDecode(((TextBox)gridViewRow.FindControl("userNameTextBox")).Text);
  763: 
  764:                     membershipUser = Membership.GetUser(providerUserKey);
  765: 
  766:                     if (userName.Length > 0)
  767:                     {
  768:                         if (Ia.Ia.Ftn.Cl.Models.Default.IsEmail(email))
  769:                         {
  770:                             membershipUser.Email = email;
  771: 
  772:                             Membership.UpdateUser(membershipUser);
  773: 
  774:                             isSuccess = true;
  775: 
  776:                             updateResultLabel.Text = "Success: User updated. ";
  777:                             updateResultLabel.CssClass = "success";
  778:                         }
  779:                         else
  780:                         {
  781:                             updateResultLabel.Text = "Error: Email is not in correct format (الإيميل غير منطقي). ";
  782:                             updateResultLabel.CssClass = "error";
  783:                         }
  784:                     }
  785:                     else
  786:                     {
  787:                         updateResultLabel.Text = "Error: UserName is not in correct format. ";
  788:                         updateResultLabel.CssClass = "error";
  789:                     }
  790:                 }
  791:                 catch (Exception ex)
  792:                 {
  793:                     updateResultLabel.Text = "Error: " + ex.Message;
  794:                     updateResultLabel.CssClass = "error";
  795:                 }
  796:             }
  797:         }
  798:         */
  799:  
  800:         ////////////////////////////////////////////////////////////////////////////
  801:  
  802:         /// <summary>
  803:         ///
  804:         /// </summary>
  805:         public static void DeleteUser(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, ref IdentityViewModel identityViewModel, string userId)
  806:         {
  807:             var identityResult = Ia.Ftn.Cl.Models.Identity.DeleteUserById(userManager, userId);
  808:  
  809:             identityViewModel.Result = new Ia.Cl.Models.Result();
  810:  
  811:             if (identityResult.Succeeded)
  812:             {
  813:                 identityViewModel.Result.AddSuccess("User deleted.");
  814:             }
  815:             else
  816:             {
  817:                 identityViewModel.Result.AddError("User not delete: " + identityResult.Errors.First().Description);
  818:             }
  819:         }
  820:  
  821:         /*
  822:         ////////////////////////////////////////////////////////////////////////////
  823: 
  824:         /// <summary>
  825:         ///
  826:         /// </summary>
  827:         public void UserGridView_RowDataBound(object sender, GridViewRowEventArgs e)
  828:         {
  829:             Ia.Ftn.Ia.Ftn.Cl.Models.Ui.Default.GridView_RowDataBound(this, sender, e);
  830:         }
  831:         */
  832:  
  833:         ////////////////////////////////////////////////////////////////////////////
  834:  
  835:         /// <summary>
  836:         ///
  837:         /// </summary>
  838:         private static void CompareCurrentAndNewViewModels(ref IdentityViewModel currentIdentityViewModel, ref IdentityViewModel identityViewModel)
  839:         {
  840:             if (identityViewModel.Framework1SelectedValue != currentIdentityViewModel.Framework1SelectedValue)
  841:             {
  842:                 identityViewModel.Framework2SelectedValue = string.Empty;
  843:                 identityViewModel.Framework3SelectedValue = string.Empty;
  844:                 identityViewModel.Framework4SelectedValue = string.Empty;
  845:                 identityViewModel.Framework5SelectedValue = string.Empty;
  846:                 identityViewModel.Framework6SelectedValue = string.Empty;
  847:             }
  848:             else if (identityViewModel.Framework2SelectedValue != currentIdentityViewModel.Framework2SelectedValue)
  849:             {
  850:                 identityViewModel.Framework3SelectedValue = string.Empty;
  851:                 identityViewModel.Framework4SelectedValue = string.Empty;
  852:                 identityViewModel.Framework5SelectedValue = string.Empty;
  853:                 identityViewModel.Framework6SelectedValue = string.Empty;
  854:             }
  855:             else if (identityViewModel.Framework3SelectedValue != currentIdentityViewModel.Framework3SelectedValue)
  856:             {
  857:                 identityViewModel.Framework4SelectedValue = string.Empty;
  858:                 identityViewModel.Framework5SelectedValue = string.Empty;
  859:                 identityViewModel.Framework6SelectedValue = string.Empty;
  860:             }
  861:             else if (identityViewModel.Framework4SelectedValue != currentIdentityViewModel.Framework4SelectedValue)
  862:             {
  863:                 identityViewModel.Framework5SelectedValue = string.Empty;
  864:                 identityViewModel.Framework6SelectedValue = string.Empty;
  865:             }
  866:             else if (identityViewModel.Framework5SelectedValue != currentIdentityViewModel.Framework5SelectedValue)
  867:             {
  868:                 identityViewModel.Framework6SelectedValue = string.Empty;
  869:             }
  870:             else if (identityViewModel.Framework6SelectedValue != currentIdentityViewModel.Framework6SelectedValue)
  871:             {
  872:             }
  873:             else
  874:             {
  875:             }
  876:         }
  877:  
  878:         ////////////////////////////////////////////////////////////////////////////
  879:         ////////////////////////////////////////////////////////////////////////////
  880:  
  881:  
  882:  
  883:  
  884:  
  885:  
  886:  
  887:  
  888:  
  889:  
  890:  
  891:  
  892:  
  893:         ////////////////////////////////////////////////////////////////////////////
  894:         ////////////////////////////////////////////////////////////////////////////
  895:  
  896:         /// <summary>
  897:         ///
  898:         /// </summary>
  899:         public static void CreateRole(RoleManager<IdentityRole> roleManager, ref IdentityViewModel identityViewModel, string roleName)
  900:         {
  901:             var identityResult = Ia.Ftn.Cl.Models.Identity.CreateRole(roleManager, roleName);
  902:  
  903:             identityViewModel.Result = new Ia.Cl.Models.Result();
  904:  
  905:             if (identityResult.Succeeded)
  906:             {
  907:                 identityViewModel.Result.AddSuccess("Role created.");
  908:             }
  909:             else
  910:             {
  911:                 identityViewModel.Result.AddError("Role not created: " + identityResult.Errors.First().Description);
  912:             }
  913:         }
  914:  
  915:         ////////////////////////////////////////////////////////////////////////////
  916:  
  917:         /// <summary>
  918:         ///
  919:         /// </summary>
  920:         public static void DeleteRole(RoleManager<IdentityRole> roleManager, ref IdentityViewModel identityViewModel, string roleName)
  921:         {
  922:             var identityResult = Ia.Ftn.Cl.Models.Identity.DeleteRole(roleManager, roleName);
  923:  
  924:             identityViewModel.Result = new Ia.Cl.Models.Result();
  925:  
  926:             if (identityResult.Succeeded)
  927:             {
  928:                 identityViewModel.Result.AddSuccess("Role deleted.");
  929:             }
  930:             else
  931:             {
  932:                 identityViewModel.Result.AddError("Role not deleted: " + identityResult.Errors.First().Description);
  933:             }
  934:         }
  935:  
  936:         ////////////////////////////////////////////////////////////////////////////
  937:  
  938:         /// <summary>
  939:         ///
  940:         /// </summary>
  941:         public static void RoleViewModel(RoleManager<IdentityRole> roleManager, ref IdentityViewModel identityViewModel)
  942:         {
  943:             string s;
  944:  
  945:             var identityRoles = Ia.Ftn.Cl.Models.Identity.RoleList(roleManager);
  946:  
  947:             identityViewModel.RoleSelectList = new SelectList(identityRoles);
  948:  
  949:             // below: list of all roles for quick reference
  950:             s = string.Empty;
  951:  
  952:             foreach (var role in identityRoles) s += role + "<br />";
  953:  
  954:             identityViewModel.RoleListText = s;
  955:         }
  956:  
  957:         ////////////////////////////////////////////////////////////////////////////
  958:         ////////////////////////////////////////////////////////////////////////////
  959:     }
  960:  
  961:     ////////////////////////////////////////////////////////////////////////////
  962:     ////////////////////////////////////////////////////////////////////////////
  963: }