)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Skip Navigation LinksHome » Code Library » Staff

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

Staff support class for Optical Fiber Network (OFN) data model.

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