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

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

Staff Support Class for Fixed Telecommunications Network (FTN) Ia.Ftn.Cl.Models.Data Model.

    1: using Microsoft.AspNetCore.Http;
    2: using Microsoft.AspNetCore.Identity;
    3: using System;
    4: using System.Collections.Generic;
    5: using System.Linq;
    6: using System.Security.Claims;
    7:  
    8: namespace Ia.Ftn.Cl.Models.Data
    9: {
   10:     ////////////////////////////////////////////////////////////////////////////
   11:  
   12:     /// <summary publish="true">
   13:     /// Staff Support Class for Fixed Telecommunications Network (FTN) Ia.Ftn.Cl.Models.Data Model.
   14:     /// </summary>
   15:     /// 
   16:     /// <remarks> 
   17:     /// Copyright © 2006-2024 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   18:     ///
   19:     /// 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
   20:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   21:     ///
   22:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   23:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   24:     /// 
   25:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   26:     /// 
   27:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   28:     /// </remarks> 
   29:     public static class StaffIdentityUser
   30:     {
   31:         private static List<Ia.Ftn.Cl.Models.StaffIdentityUser> staffIdentityUserList;
   32:  
   33:         private static readonly object objectLock = new object();
   34:  
   35:         /////////////////////////////////////////////////////////////////////////////////
   36:  
   37:         /// <summary>
   38:         ///
   39:         /// </summary>
   40:         public static Ia.Ftn.Cl.Models.StaffIdentityUser CurrentStaffIdentityUser(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, ClaimsPrincipal User)
   41:         {
   42:             var staffIdentityUser = userManager.GetUserAsync(User).Result;
   43:  
   44:             if (staffIdentityUser != null)
   45:             {
   46:                 var list = Ia.Ftn.Cl.Models.Data.StaffIdentityUser.List();
   47:  
   48:                 staffIdentityUser = (from l in list where l.Id == staffIdentityUser.Id select l).SingleOrDefault();
   49:             }
   50:  
   51:             return staffIdentityUser;
   52:         }
   53:  
   54:         ////////////////////////////////////////////////////////////////////////////
   55:  
   56:         /// <summary>
   57:         ///
   58:         /// </summary>
   59:         public static List<Ia.Ftn.Cl.Models.StaffIdentityUser> List()
   60:         {
   61:             if (staffIdentityUserList == null || staffIdentityUserList.Count == 0)
   62:             {
   63:                 lock (objectLock)
   64:                 {
   65:                     staffIdentityUserList = Ia.Ftn.Cl.Models.Data.StaffIdentityUser._List();
   66:                 }
   67:             }
   68:  
   69:             return staffIdentityUserList;
   70:         }
   71:  
   72:         ////////////////////////////////////////////////////////////////////////////
   73:  
   74:         /// <summary>
   75:         ///
   76:         /// </summary>
   77:         private static List<Ia.Ftn.Cl.Models.StaffIdentityUser> _List()
   78:         {
   79:             string frameworkId;
   80:             string userId;
   81:             List<Ia.Ftn.Cl.Models.StaffIdentityUser> staffIdentityUserList;
   82:  
   83:             staffIdentityUserList = Ia.Ftn.Cl.Models.Identity.StaffIdentityUserList();
   84:  
   85:             var frameworkList = Ia.Ftn.Cl.Models.Data.Administration.FrameworkList;
   86:  
   87:             if (staffIdentityUserList != null && staffIdentityUserList.Count > 0)
   88:             {
   89:                 if (staffIdentityUserList.Count > 0)
   90:                 {
   91:                     // below: the order of the multiple foreach loops is imporant
   92:  
   93:                     for (int i = 0; i < staffIdentityUserList.Count; i++)
   94:                     {
   95:                         frameworkId = staffIdentityUserList[i].FrameworkId;
   96:                         userId = staffIdentityUserList[i].Id;
   97:  
   98:                         if (!string.IsNullOrEmpty(frameworkId))
   99:                         {
  100:                             staffIdentityUserList[i].Framework = (from u in frameworkList
  101:                                                                   where u.Id == frameworkId
  102:                                                                   select u).SingleOrDefault();
  103:                         }
  104:                     }
  105:  
  106:                     for (int i = 0; i < staffIdentityUserList.Count; i++)
  107:                     {
  108:                         frameworkId = staffIdentityUserList[i].FrameworkId;
  109:  
  110:                         if (!string.IsNullOrEmpty(frameworkId))
  111:                         {
  112:                             if (staffIdentityUserList[i].Framework.Parent != null)
  113:                             {
  114:                                 staffIdentityUserList[i].Head = (from s in staffIdentityUserList
  115:                                                                  where s.IsHead == true && (staffIdentityUserList[i].IsHead == true && s.FrameworkId == staffIdentityUserList[i].Framework.Parent.Id || staffIdentityUserList[i].IsHead == false && s.FrameworkId == staffIdentityUserList[i].Framework.Id)
  116:                                                                  select s).SingleOrDefault();
  117:                             }
  118:                         }
  119:                     }
  120:  
  121:                     for (int i = 0; i < staffIdentityUserList.Count; i++)
  122:                     {
  123:                         frameworkId = staffIdentityUserList[i].FrameworkId;
  124:  
  125:                         if (!string.IsNullOrEmpty(frameworkId))
  126:                         {
  127:                             // below: Subordinates
  128:                             if (staffIdentityUserList[i].IsHead)
  129:                             {
  130:                                 staffIdentityUserList[i].Subordinates = (from s in staffIdentityUserList
  131:                                                                          where s.Id != staffIdentityUserList[i].Id &&
  132:                                                                          (s.Framework != null && s.Framework == staffIdentityUserList[i].Framework && s.IsHead == false)
  133:                                                                          ||
  134:                                                                          (
  135:                                                                          s.Framework != null &&
  136:                                                                          s.Framework.Parent != null &&
  137:                                                                          (s.Framework.Parent == staffIdentityUserList[i].Framework || s.Framework.Parent.Parent != null &&
  138:                                                                           (s.Framework.Parent.Parent == staffIdentityUserList[i].Framework || s.Framework.Parent.Parent.Parent != null &&
  139:                                                                            (s.Framework.Parent.Parent.Parent == staffIdentityUserList[i].Framework || s.Framework.Parent.Parent.Parent != null &&
  140:                                                                             (s.Framework.Parent.Parent.Parent.Parent == staffIdentityUserList[i].Framework)
  141:                                                                            )
  142:                                                                           )
  143:                                                                          )
  144:                                                                          )
  145:                                                                          select s).ToList();
  146:                             }
  147:                             else
  148:                             {
  149:                                 staffIdentityUserList[i].Subordinates = new List<Ia.Ftn.Cl.Models.StaffIdentityUser>();
  150:                             }
  151:  
  152:                             staffIdentityUserList[i].SubordinatesOrSelf = (from s in staffIdentityUserList where s.Id == staffIdentityUserList[i].Id select s).Union(staffIdentityUserList[i].Subordinates).ToList();
  153:  
  154:  
  155:                             // below: Colleagues
  156:                             if (staffIdentityUserList[i].IsHead)
  157:                             {
  158:                                 staffIdentityUserList[i].Colleagues = (from s in staffIdentityUserList
  159:                                                                        where s.Id != staffIdentityUserList[i].Id
  160:                                                                        && s.IsHead
  161:                                                                        && s.Framework.Parent != null && staffIdentityUserList[i].Framework.Parent != null
  162:                                                                        && s.Framework.Parent == staffIdentityUserList[i].Framework.Parent
  163:                                                                        select s).ToList();
  164:                             }
  165:                             else
  166:                             {
  167:                                 staffIdentityUserList[i].Colleagues = (from s in staffIdentityUserList
  168:                                                                        where s.Id != staffIdentityUserList[i].Id
  169:                                                                        && !s.IsHead
  170:                                                                        && s.Framework.Parent == staffIdentityUserList[i].Framework.Parent
  171:                                                                        select s).ToList();
  172:                             }
  173:  
  174:                             staffIdentityUserList[i].ColleaguesOrSelf = (from s in staffIdentityUserList
  175:                                                                          where s.Id == staffIdentityUserList[i].Id
  176:                                                                          select s).Union(staffIdentityUserList[i].Colleagues).ToList();
  177:  
  178:  
  179:                             // below: Heads
  180:                             staffIdentityUserList[i].Heads = new List<Ia.Ftn.Cl.Models.StaffIdentityUser>();
  181:  
  182:                             foreach (var ancestor in staffIdentityUserList[i].Framework.AncestorsOrSelf)
  183:                             {
  184:                                 var head = (from s in staffIdentityUserList where s.Id != staffIdentityUserList[i].Id && s.Framework == ancestor && s.IsHead select s).SingleOrDefault();
  185:  
  186:                                 if (head != null) staffIdentityUserList[i].Heads.Add(head);
  187:                             }
  188:  
  189:                             staffIdentityUserList[i].HeadsOrSelf = (from s in staffIdentityUserList where s.Id == staffIdentityUserList[i].Id select s).Union(staffIdentityUserList[i].Heads).ToList();
  190:                         }
  191:                     }
  192:                 }
  193:  
  194:                 staffIdentityUserList = (from s in staffIdentityUserList select s).OrderByDescending(c => c.IsHead).ThenBy(c => c.FrameworkId).ToList();
  195:             }
  196:             else
  197:             {
  198:                 //throw new ArgumentOutOfRangeException(@"Ia.Ftn.Cl.Model.Data.Staff._List(): userList == null or userList.Count == 0");
  199:             }
  200:  
  201:             return staffIdentityUserList;
  202:         }
  203:  
  204:         ////////////////////////////////////////////////////////////////////////////
  205:  
  206:         /// <summary>
  207:         ///
  208:         /// </summary>
  209:         [Obsolete]
  210:         public static bool Create(Ia.Ftn.Cl.Models.StaffIdentityUser newStaff, out Ia.Cl.Models.Result result)
  211:         {
  212:             var b = false;
  213:             result = new Ia.Cl.Models.Result();
  214:  
  215:             using (var db = new Ia.Ftn.Cl.Db())
  216:             {
  217:                 newStaff.Created = newStaff.Updated = DateTime.UtcNow.AddHours(3);
  218:  
  219:                 db.StaffIdentityUsers.Add(newStaff);
  220:                 db.SaveChanges();
  221:  
  222:                 b = true;
  223:                 result.AddSuccess("Staff " + newStaff.FirstAndMiddleName + " created.");
  224:  
  225:                 staffIdentityUserList = null;
  226:             }
  227:  
  228:             return b;
  229:         }
  230:  
  231:         ////////////////////////////////////////////////////////////////////////////
  232:  
  233:         /// <summary>
  234:         ///
  235:         /// </summary>
  236:         [Obsolete]
  237:         public static Ia.Ftn.Cl.Models.StaffIdentityUser Read(string id)
  238:         {
  239:             Ia.Ftn.Cl.Models.StaffIdentityUser staff;
  240:  
  241:             using (var db = new Ia.Ftn.Cl.Db())
  242:             {
  243:                 staff = (from s in db.StaffIdentityUsers where s.Id == id select s).SingleOrDefault();
  244:             }
  245:  
  246:             return staff;
  247:         }
  248:  
  249:         ////////////////////////////////////////////////////////////////////////////
  250:  
  251:         /// <summary>
  252:         ///
  253:         /// </summary>
  254:         [Obsolete]
  255:         public static List<Ia.Ftn.Cl.Models.StaffIdentityUser> ReadList()
  256:         {
  257:             List<Ia.Ftn.Cl.Models.StaffIdentityUser> list;
  258:  
  259:             using (var db = new Ia.Ftn.Cl.Db())
  260:             {
  261:                 list = (from s in db.StaffIdentityUsers select s).ToList();
  262:             }
  263:  
  264:             return list.ToList();
  265:         }
  266:  
  267:         ////////////////////////////////////////////////////////////////////////////
  268:  
  269:         /// <summary>
  270:         ///
  271:         /// </summary>
  272:         [Obsolete]
  273:         public static bool Update(Ia.Ftn.Cl.Models.StaffIdentityUser updatedStaff, out Ia.Cl.Models.Result result)
  274:         {
  275:             bool b;
  276:             Ia.Ftn.Cl.Models.StaffIdentityUser staff;
  277:  
  278:             b = false;
  279:             result = new Ia.Cl.Models.Result();
  280:  
  281:             using (var db = new Ia.Ftn.Cl.Db())
  282:             {
  283:                 staff = (from s in db.StaffIdentityUsers where s.Id == updatedStaff.Id select s).SingleOrDefault();
  284:  
  285:                 if (staff.Update(updatedStaff))
  286:                 {
  287:                     db.StaffIdentityUsers.Attach(staff);
  288:                     db.Entry(staff).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  289:                 }
  290:  
  291:                 db.SaveChanges();
  292:  
  293:                 b = true;
  294:                 result.AddSuccess("Staff " + updatedStaff.FirstAndMiddleName + " updated.");
  295:  
  296:                 staffIdentityUserList = null;
  297:             }
  298:  
  299:             return b;
  300:         }
  301:  
  302:         ////////////////////////////////////////////////////////////////////////////
  303:  
  304:         /// <summary>
  305:         ///
  306:         /// </summary>
  307:         [Obsolete]
  308:         public static bool AssignUserId(string staffId, string userId)
  309:         {
  310:             bool b;
  311:             Ia.Ftn.Cl.Models.StaffIdentityUser updatedStaff;
  312:  
  313:             b = false;
  314:  
  315:             using (var db = new Ia.Ftn.Cl.Db())
  316:             {
  317:                 updatedStaff = (from s in db.StaffIdentityUsers where s.Id == staffId select s).SingleOrDefault();
  318:  
  319:                 if (updatedStaff != null)
  320:                 {
  321:                     updatedStaff.Id = userId;
  322:                     updatedStaff.Updated = DateTime.UtcNow.AddHours(3);
  323:  
  324:                     db.StaffIdentityUsers.Attach(updatedStaff);
  325:  
  326:                     var v = db.Entry(updatedStaff);
  327:                     v.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  328:                     db.SaveChanges();
  329:  
  330:                     b = true;
  331:  
  332:                     staffIdentityUserList = null;
  333:                 }
  334:                 else
  335:                 {
  336:                     b = false;
  337:                 }
  338:             }
  339:  
  340:             return b;
  341:         }
  342:  
  343:         ////////////////////////////////////////////////////////////////////////////
  344:  
  345:         /// <summary>
  346:         ///
  347:         /// </summary>
  348:         [Obsolete]
  349:         public static bool Delete(string id, out Ia.Cl.Models.Result result)
  350:         {
  351:             bool b;
  352:  
  353:             b = false;
  354:             result = new Ia.Cl.Models.Result();
  355:  
  356:             using (var db = new Ia.Ftn.Cl.Db())
  357:             {
  358:                 var staff = (from s in db.StaffIdentityUsers
  359:                              where s.Id == id
  360:                              select s).FirstOrDefault();
  361:  
  362:                 db.StaffIdentityUsers.Remove(staff);
  363:                 db.SaveChanges();
  364:  
  365:                 b = true;
  366:                 result.AddSuccess("Staff " + staff.FirstAndMiddleName + " deleted.");
  367:  
  368:                 staffIdentityUserList = null;
  369:             }
  370:  
  371:             return b;
  372:         }
  373:  
  374:         /*
  375:         ////////////////////////////////////////////////////////////////////////////
  376:         ////////////////////////////////////////////////////////////////////////////
  377: 
  378:         /// <summary>
  379:         ///
  380:         /// </summary>
  381:         [Obsolete]
  382:         public static void CreateStaff(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, RoleManager<IdentityRole> roleManager, Ia.Ftn.Cl.Models.StaffIdentityUser creatorStaff, string firstName, string middleName, string lastName, string frameworkId, bool isHead, string email, string userName, string password, string confirmPassword, out Ia.Cl.Models.Result result)
  383:         {
  384:             bool b;
  385:             string userId;
  386:             DateTime now;
  387:             Ia.Ftn.Cl.Models.StaffIdentityUser _staff;
  388:             Ia.Ftn.Cl.Models.Business.Administration.Framework framework;
  389: 
  390:             b = false;
  391:             result = new Ia.Cl.Models.Result();
  392: 
  393:             if (!string.IsNullOrEmpty(firstName))
  394:             {
  395:                 if (!string.IsNullOrEmpty(lastName))
  396:                 {
  397:                     if (!string.IsNullOrEmpty(frameworkId))
  398:                     {
  399:                         framework = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList
  400:                                      where f.Id == frameworkId
  401:                                      select f).Single();
  402: 
  403:                         if (!Ia.Ftn.Cl.Models.Business.Authority.FrameworkIsApplication(framework)
  404:                             || Ia.Ftn.Cl.Models.Business.Authority.FrameworkIsApplication(framework) && Ia.Ftn.Cl.Models.Business.Authority.StaffIsApplication(creatorStaff)
  405:                             || Ia.Ftn.Cl.Models.Business.Authority.FrameworkIsApplication(framework) && creatorStaff == null)
  406:                         {
  407:                             if (!isHead || isHead && !Ia.Ftn.Cl.Models.Business.Administration.FrameworkHasAHead(userManager, framework))
  408:                             {
  409:                                 now = DateTime.UtcNow.AddHours(3);
  410: 
  411:                                 var identityUser = userManager.FindByNameAsync(userName).Result;
  412: 
  413:                                 if (identityUser == null)
  414:                                 {
  415:                                     if (string.IsNullOrEmpty(email)) email = userName + "@kuix.com";
  416: 
  417:                                     identityUser = Ia.Ftn.Cl.Models.Data.StaffIdentityUser.CreateUser(userManager, email, userName, password, confirmPassword, out result);
  418: 
  419:                                     if (result.IsSuccessful)
  420:                                     {
  421:                                         userId = identityUser.Id;
  422: 
  423:                                         _staff = new Ia.Ftn.Cl.Models.StaffIdentityUser()
  424:                                         {
  425:                                             FirstName = firstName,
  426:                                             MiddleName = middleName,
  427:                                             LastName = lastName,
  428:                                             IsHead = isHead,
  429:                                             FrameworkId = frameworkId,
  430:                                             Created = now,
  431:                                             Updated = now
  432:                                         };
  433: 
  434:                                         b = Ia.Ftn.Cl.Models.Data.StaffIdentityUser.Create(_staff, out result);
  435: 
  436:                                         if (b)
  437:                                         {
  438:                                             _staff = (from s in Ia.Ftn.Cl.Models.Data.StaffIdentityUser.List(userManager)
  439:                                                       where s.Id == userId
  440:                                                       select s).SingleOrDefault();
  441: 
  442:                                             Ia.Ftn.Cl.Models.Business.Authority.UpdateFrameworkRolesOfStaffIdentityUser(userManager, roleManager, _staff);
  443: 
  444:                                             result.AddSuccess(@"User created: " + userName + " (" + _staff.FullName + ")");
  445:                                         }
  446:                                         else
  447:                                         {
  448:                                             result.AddError(@"CreateUser() succeeded but Staff.Create() failed.");
  449:                                         }
  450:                                     }
  451:                                     else
  452:                                     {
  453:                                         result.AddError("");
  454:                                     }
  455:                                 }
  456:                                 else
  457:                                 {
  458:                                     result.AddError(@"User """ + userName + @""" already exists.");
  459:                                 }
  460:                             }
  461:                             else
  462:                             {
  463:                                 result.AddError("Can't create staff as head because framework already has a head.");
  464:                             }
  465:                         }
  466:                         else
  467:                         {
  468:                             result.AddError("Can't create staff application.");
  469:                         }
  470:                     }
  471:                     else
  472:                     {
  473:                         result.AddError("frameworkId <= 0.");
  474:                     }
  475:                 }
  476:                 else
  477:                 {
  478:                     result.AddError("Last name is null or empty.");
  479:                 }
  480:             }
  481:             else
  482:             {
  483:                 result.AddError("First name is null or empty.");
  484:             }
  485:         }
  486:         */
  487:  
  488:         ////////////////////////////////////////////////////////////////////////////
  489:  
  490:         /// <summary>
  491:         ///
  492:         /// </summary>
  493:         public static Ia.Ftn.Cl.Models.StaffIdentityUser CreateApplicationStaff(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, RoleManager<IdentityRole> roleManager, string firstName, string middleName, string lastName, string email, string userName, string phoneNumber, string password, out IdentityResult identityResult)
  494:         {
  495:             var isHead = true;
  496:  
  497:             var frameworkId = Ia.Ftn.Cl.Models.Business.Authority.FrameworkApplicationId();
  498:  
  499:             return Ia.Ftn.Cl.Models.Identity.CreateUser(userManager, firstName, middleName, lastName, frameworkId, isHead, email, userName, phoneNumber, password, out identityResult);
  500:         }
  501:  
  502:         /*
  503:         ////////////////////////////////////////////////////////////////////////////
  504: 
  505:         /// <summary>
  506:         ///
  507:         /// </summary>
  508:         [Obsolete]
  509:         public static Ia.Ftn.Cl.Models.StaffIdentityUser CreateUser(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, string email, string userName, string password, string confirmPassword, out Ia.Cl.Models.Result result)
  510:         {
  511:             Ia.Ftn.Cl.Models.StaffIdentityUser identityUser;
  512:             IdentityResult identityResult;
  513: 
  514:             var passwordMinLength = 4; // in web.config, system.web/membership/providers/DefaultMembershipProvider: minRequiredPasswordLength="4"
  515: 
  516:             identityUser = null;
  517:             result = new Ia.Cl.Models.Result();
  518: 
  519:             if (!string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(confirmPassword))
  520:             {
  521:                 if (password.Length >= passwordMinLength && confirmPassword.Length >= passwordMinLength)
  522:                 {
  523:                     if (password == confirmPassword)
  524:                     {
  525:                         identityUser = userManager.FindByNameAsync(userName).Result;
  526: 
  527:                         if (identityUser == null)
  528:                         {
  529:                             identityUser = Ia.Ftn.Cl.Models.Identity.CreateUser((userManager, userName, password, email, out identityResult);
  530: 
  531:                             if (identityResult.Succeeded)
  532:                             {
  533:                                 result.AddSuccess("User created.");
  534:                             }
  535:                             else
  536:                             {
  537:                                 result.AddError("User not created.");
  538:                             }
  539:                         }
  540:                         else
  541:                         {
  542:                             result.AddError(@"User """ + userName + @""" already exists.");
  543:                         }
  544:                     }
  545:                     else
  546:                     {
  547:                         result.AddError("Password and its verification do not match.");
  548:                     }
  549:                 }
  550:                 else
  551:                 {
  552:                     result.AddError("New password is too short, less than " + passwordMinLength + ".");
  553:                 }
  554:             }
  555:             else
  556:             {
  557:                 result.AddError("Password is null or empty.");
  558:             }
  559: 
  560:             return identityUser;
  561:         }
  562:         */
  563:  
  564:         /*
  565:         ////////////////////////////////////////////////////////////////////////////
  566:         ////////////////////////////////////////////////////////////////////////////
  567: 
  568:         /// <summary>
  569:         ///
  570:         /// </summary>
  571:         public static Ia.Ftn.Cl.Models.StaffIdentityUser StaffIdentityUser(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, string userId)
  572:         {
  573:             Ia.Ftn.Cl.Models.StaffIdentityUser staffIdentityUser;
  574: 
  575:             //if (HttpContext.Current != null && HttpContext.Current.Session["staffIdentityUser"] != null)
  576:             if (Ia.Ftn.Cl.Models.Cache.Instance.TryGetValue("staffIdentityUser", out staffIdentityUser))
  577:             {
  578:             }
  579:             else
  580:             {
  581:                 lock (objectLock)
  582:                 {
  583:                     staffIdentityUser = Ia.Ftn.Cl.Models.Data.StaffIdentityUser._IdentityUser(userManager, userId);
  584: 
  585:                     //if (HttpContext.Current != null) HttpContext.Current.Session["staffIdentityUser"] = staffIdentityUser;
  586:                     Ia.Ftn.Cl.Models.Cache.Instance.Set("staffIdentityUser", staffIdentityUser);
  587:                 }
  588:             }
  589: 
  590:             return staffIdentityUser;
  591:         }
  592: 
  593:         ////////////////////////////////////////////////////////////////////////////
  594: 
  595:         /// <summary>
  596:         ///
  597:         /// </summary>
  598:         private static Ia.Ftn.Cl.Models.StaffIdentityUser _IdentityUser(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, string userId)
  599:         {
  600:             Ia.Ftn.Cl.Models.StaffIdentityUser staffIdentityUser;
  601: 
  602:             var identityUser = userManager.FindByIdAsync(userId).Result;
  603: 
  604:             if (identityUser != null)
  605:             {
  606:                 if (Guid.TryParse(identityUser.Id, out Guid guid))
  607:                 {
  608:                     staffIdentityUser = (from s in Ia.Ftn.Cl.Models.Data.StaffIdentityUser.List(userManager)
  609:                                          where s.Id == guid.ToString()
  610:                                          select s).SingleOrDefault();
  611:                 }
  612:                 else staffIdentityUser = null;
  613:             }
  614:             else staffIdentityUser = null;
  615: 
  616:             return staffIdentityUser;
  617:         }
  618: 
  619:         ////////////////////////////////////////////////////////////////////////////
  620: 
  621:         /// <summary>
  622:         ///
  623:         /// </summary>
  624:         public static Ia.Ftn.Cl.Models.StaffIdentityUser IdentityUser2(UserManager<Ia.Ftn.Cl.Models.StaffIdentityUser> userManager, string userId)
  625:         {
  626:             Ia.Ftn.Cl.Models.StaffIdentityUser staffIdentityUser;
  627: 
  628:             if (!string.IsNullOrEmpty(userId))
  629:             {
  630:                 staffIdentityUser = (from s in Ia.Ftn.Cl.Models.Data.StaffIdentityUser.List(userManager)
  631:                                      where s.Id == userId
  632:                                      select s).SingleOrDefault();
  633:             }
  634:             else staffIdentityUser = null;
  635: 
  636:             return staffIdentityUser;
  637:         }
  638:         */
  639:  
  640:         ////////////////////////////////////////////////////////////////////////////
  641:  
  642:         /// <summary>
  643:         ///
  644:         /// </summary>
  645:         [Obsolete]
  646:         public static bool NullifyUserId(string staffId)
  647:         {
  648:             bool b;
  649:             Ia.Ftn.Cl.Models.StaffIdentityUser staff;
  650:  
  651:             using (var db = new Ia.Ftn.Cl.Db())
  652:             {
  653:                 staff = (from s in db.StaffIdentityUsers where s.Id == staffId select s).SingleOrDefault();
  654:  
  655:                 staff.Id = Guid.Empty.ToString();
  656:  
  657:                 db.StaffIdentityUsers.Attach(staff);
  658:                 db.Entry(staff).Property(u => u.Id).IsModified = true;
  659:  
  660:                 db.SaveChanges();
  661:  
  662:                 b = true;
  663:  
  664:                 staffIdentityUserList = null;
  665:             }
  666:  
  667:             return b;
  668:         }
  669:  
  670:         ////////////////////////////////////////////////////////////////////////////
  671:  
  672:         /// <summary>
  673:         ///
  674:         /// </summary>
  675:         public static void UnlockoutAllIdentityUsers()
  676:         {
  677:             using (var db = new Ia.Ftn.Cl.Db())
  678:             {
  679:                 //db.Database.ExecuteSqlRaw("update Memberships set IsLockedOut = 0;");
  680:             }
  681:         }
  682:  
  683:         ////////////////////////////////////////////////////////////////////////////
  684:  
  685:         /// <summary>
  686:         ///
  687:         /// </summary>
  688:         public static void SetMembershipIsApprovedToFalseIfUserLastActivityDateIsLessThanDateTime(DateTime dateTime)
  689:         {
  690:             using (var db = new Ia.Ftn.Cl.Db())
  691:             {
  692:                 //db.Database.ExecuteSqlRaw("update Memberships set IsApproved = 0 where UserId in (select u.StaffIdentityUser.Id from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
  693:             }
  694:         }
  695:  
  696:         ////////////////////////////////////////////////////////////////////////////
  697:  
  698:         /// <summary>
  699:         ///
  700:         /// </summary>
  701:         public static void DeleteStaffIfUserLastActivityDateIsLessThanDateTime(DateTime dateTime)
  702:         {
  703:             using (var db = new Ia.Ftn.Cl.Db())
  704:             {
  705:                 //db.Database.ExecuteSqlRaw("delete Staffs where UserId in (select u.StaffIdentityUser.Id from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
  706:                 //db.Database.ExecuteSqlRaw("delete Memberships where UserId in (select u.StaffIdentityUser.Id from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
  707:                 //db.Database.ExecuteSqlRaw("delete UsersInRoles where UserId in (select u.StaffIdentityUser.Id from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
  708:                 //db.Database.ExecuteSqlRaw("delete Profiles where UserId in (select u.StaffIdentityUser.Id from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
  709:                 //db.Database.ExecuteSqlRaw("delete Users where UserId in (select u.StaffIdentityUser.Id from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
  710:             }
  711:         }
  712:  
  713:         ////////////////////////////////////////////////////////////////////////////
  714:  
  715:         /// <summary>
  716:         ///
  717:         /// </summary>
  718:         public static void RemoveInactiveStaff(DateTime dateTime)
  719:         {
  720:             using (var db = new Ia.Ftn.Cl.Db())
  721:             {
  722:                 //var list = Ia.Ftn.Cl.Models.Identity.InactiveSinceDateTimeUserList(dateTime);
  723:  
  724:                 //db.Database.ExecuteSqlRaw("update Memberships set IsLockedOut = 0;");
  725:             }
  726:         }
  727:  
  728:         ////////////////////////////////////////////////////////////////////////////
  729:         ////////////////////////////////////////////////////////////////////////////
  730:     }
  731:  
  732:     ////////////////////////////////////////////////////////////////////////////
  733:     ////////////////////////////////////////////////////////////////////////////
  734: }