)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Staff (Ia.Ftn.Cl.Model.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) data model.

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