1: using Microsoft.EntityFrameworkCore;
2: using System;
3: using System.Collections.Generic;
4: using System.Linq;
5: using System.Web;
6: using System.Web.Security;
7:
8: namespace Ia.Ngn.Cl.Model.Data
9: {
10: ////////////////////////////////////////////////////////////////////////////
11:
12: /// <summary publish="true">
13: /// Staff support class for Optical Fiber Network (OFN) data model.
14: /// </summary>
15: ///
16: /// <remarks>
17: /// Copyright © 2006-2020 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 Staff
30: {
31: private const int initialStaffListLength = 200;
32: private static List<Ia.Ngn.Cl.Model.Staff> staffList;
33: private static DateTime staffUserListUpdateDateTime;
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.Ngn.Cl.Model.Staff> List
45: {
46: get
47: {
48: if (staffList == null || staffList.Count == 0 || StaffUserListUpdateDateTime != Ia.Cl.Model.Identity.UserListTimestamp)
49: {
50: lock (objectLock)
51: {
52: staffList = Ia.Ngn.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.Ngn.Cl.Model.Staff> _List()
66: {
67: int frameworkId;
68: Guid userId;
69:
70: var staffList = new List<Ia.Ngn.Cl.Model.Staff>(initialStaffListLength);
71:
72: using (var db = new Ia.Ngn.Cl.Model.Ngn())
73: {
74: var userList = Ia.Cl.Model.Identity.UserList;
75:
76: var frameworkList = Ia.Ngn.Cl.Model.Data.Administration.FrameworkList;
77:
78: if (userList != null && userList.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].UserId;
90:
91: staffList[i].User = (from u in userList
92: where userId != Guid.Empty && u.ProviderUserKey == userId
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.Ngn.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.Ngn.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: staffUserListUpdateDateTime = Ia.Cl.Model.Identity.UserListTimestamp;
178: }
179: else
180: {
181: throw new ArgumentOutOfRangeException(@"Ia.Ngn.Cl.Model.Data.Staff._List(): userList == null or userList.Count == 0");
182: }
183:
184: return staffList;
185: }
186: }
187:
188: ////////////////////////////////////////////////////////////////////////////
189:
190: /// <summary>
191: ///
192: /// </summary>
193: public static DateTime StaffUserListUpdateDateTime
194: {
195: get
196: {
197: return staffUserListUpdateDateTime;
198: }
199: }
200:
201: ////////////////////////////////////////////////////////////////////////////
202:
203: /// <summary>
204: ///
205: /// </summary>
206: public static bool Create(Ia.Ngn.Cl.Model.Staff newStaff, out Ia.Cl.Model.Result result)
207: {
208: bool b;
209:
210: b = false;
211: result = new Ia.Cl.Model.Result();
212:
213: using (var db = new Ia.Ngn.Cl.Model.Ngn())
214: {
215: newStaff.Created = newStaff.Updated = DateTime.UtcNow.AddHours(3);
216:
217: db.Staffs.Add(newStaff);
218: db.SaveChanges();
219:
220: b = true;
221: result.AddSuccess("Staff " + newStaff.FirstAndMiddleName + " created.");
222:
223: staffList = null;
224: }
225:
226: return b;
227: }
228:
229: ////////////////////////////////////////////////////////////////////////////
230:
231: /// <summary>
232: ///
233: /// </summary>
234: public static Ia.Ngn.Cl.Model.Staff Read(int id)
235: {
236: Ia.Ngn.Cl.Model.Staff staff;
237:
238: using (var db = new Ia.Ngn.Cl.Model.Ngn())
239: {
240: staff = (from s in db.Staffs where s.Id == id select s).SingleOrDefault();
241: }
242:
243: return staff;
244: }
245:
246: ////////////////////////////////////////////////////////////////////////////
247:
248: /// <summary>
249: ///
250: /// </summary>
251: public static List<Ia.Ngn.Cl.Model.Staff> ReadList()
252: {
253: List<Ia.Ngn.Cl.Model.Staff> list;
254:
255: using (var db = new Ia.Ngn.Cl.Model.Ngn())
256: {
257: list = (from s in db.Staffs select s).ToList();
258: }
259:
260: return list.ToList();
261: }
262:
263: ////////////////////////////////////////////////////////////////////////////
264:
265: /// <summary>
266: ///
267: /// </summary>
268: public static bool Update(Ia.Ngn.Cl.Model.Staff updatedStaff, out Ia.Cl.Model.Result result)
269: {
270: bool b;
271: Ia.Ngn.Cl.Model.Staff staff;
272:
273: b = false;
274: result = new Ia.Cl.Model.Result();
275:
276: using (var db = new Ia.Ngn.Cl.Model.Ngn())
277: {
278: staff = (from s in db.Staffs where s.Id == updatedStaff.Id select s).SingleOrDefault();
279:
280: if (staff.Update(updatedStaff))
281: {
282: db.Staffs.Attach(staff);
283: db.Entry(staff).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
284: }
285:
286: db.SaveChanges();
287:
288: b = true;
289: result.AddSuccess("Staff " + updatedStaff.FirstAndMiddleName + " updated.");
290:
291: staffList = null;
292: }
293:
294: return b;
295: }
296:
297: ////////////////////////////////////////////////////////////////////////////
298:
299: /// <summary>
300: ///
301: /// </summary>
302: public static bool AssignUserId(int staffId, Guid userId)
303: {
304: bool b;
305: Ia.Ngn.Cl.Model.Staff updatedStaff;
306:
307: b = false;
308:
309: using (var db = new Ia.Ngn.Cl.Model.Ngn())
310: {
311: updatedStaff = (from s in db.Staffs where s.Id == staffId select s).SingleOrDefault();
312:
313: if (updatedStaff != null)
314: {
315: updatedStaff.UserId = userId;
316: updatedStaff.Updated = DateTime.UtcNow.AddHours(3);
317:
318: db.Staffs.Attach(updatedStaff);
319:
320: var v = db.Entry(updatedStaff);
321: v.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
322: db.SaveChanges();
323:
324: b = true;
325:
326: staffList = null;
327: }
328: else
329: {
330: b = false;
331: }
332: }
333:
334: return b;
335: }
336:
337: ////////////////////////////////////////////////////////////////////////////
338:
339: /// <summary>
340: ///
341: /// </summary>
342: public static bool Delete(int id, out Ia.Cl.Model.Result result)
343: {
344: bool b;
345:
346: b = false;
347: result = new Ia.Cl.Model.Result();
348:
349: using (var db = new Ia.Ngn.Cl.Model.Ngn())
350: {
351: var staff = (from s in db.Staffs
352: where s.Id == id
353: select s).FirstOrDefault();
354:
355: db.Staffs.Remove(staff);
356: db.SaveChanges();
357:
358: b = true;
359: result.AddSuccess("Staff " + staff.FirstAndMiddleName + " deleted.");
360:
361: staffList = null;
362: }
363:
364: return b;
365: }
366:
367: ////////////////////////////////////////////////////////////////////////////
368:
369: /// <summary>
370: ///
371: /// </summary>
372: public static Ia.Ngn.Cl.Model.Staff MembershipUser
373: {
374: get
375: {
376: Ia.Ngn.Cl.Model.Staff staffMembershipUser;
377:
378: if (HttpContext.Current != null && HttpContext.Current.Session["staffMembershipUser"] != null)
379: {
380: staffMembershipUser = HttpContext.Current.Session["staffMembershipUser"] as Ia.Ngn.Cl.Model.Staff;
381: }
382: else
383: {
384: lock (objectLock)
385: {
386: staffMembershipUser = Ia.Ngn.Cl.Model.Data.Staff._MembershipUser;
387:
388: if (HttpContext.Current != null) HttpContext.Current.Session["staffMembershipUser"] = staffMembershipUser;
389: }
390: }
391:
392: if (staffMembershipUser != null)
393: {
394: if (!staffIpDictionary.ContainsKey(staffMembershipUser.UserId.ToString()))
395: {
396: staffIpDictionary[staffMembershipUser.UserId.ToString()] = Ia.Cl.Model.Default.RequestUserIP();
397: }
398: }
399:
400: return staffMembershipUser;
401: }
402: }
403:
404: ////////////////////////////////////////////////////////////////////////////
405:
406: /// <summary>
407: ///
408: /// </summary>
409: public static void ClearStaffMembershipUserSession()
410: {
411: if (HttpContext.Current != null && HttpContext.Current.Session["staffMembershipUser"] != null)
412: {
413: lock (objectLock)
414: {
415: HttpContext.Current.Session.Remove("staffMembershipUser");
416: }
417: }
418: }
419:
420: ////////////////////////////////////////////////////////////////////////////
421:
422: /// <summary>
423: ///
424: /// </summary>
425: private static Ia.Ngn.Cl.Model.Staff _MembershipUser
426: {
427: get
428: {
429: Ia.Ngn.Cl.Model.Staff staffMembershipUser;
430:
431: if (Membership.GetUser() != null)
432: {
433: if (Guid.TryParse(Membership.GetUser().ProviderUserKey.ToString(), out Guid guid))
434: {
435: staffMembershipUser = (from s in Ia.Ngn.Cl.Model.Data.Staff.List where s.User != null && s.UserId == guid select s).SingleOrDefault();
436: }
437: else staffMembershipUser = null;
438: }
439: else staffMembershipUser = null;
440:
441: return staffMembershipUser;
442: }
443: }
444:
445: ////////////////////////////////////////////////////////////////////////////
446:
447: /// <summary>
448: ///
449: /// </summary>
450: public static Ia.Ngn.Cl.Model.Staff MembershipUser2(Guid userId)
451: {
452: Ia.Ngn.Cl.Model.Staff staffMembershipUser;
453:
454: if (userId != Guid.Empty)
455: {
456: staffMembershipUser = (from s in Ia.Ngn.Cl.Model.Data.Staff.List where s.UserId == userId select s).SingleOrDefault();
457: }
458: else staffMembershipUser = null;
459:
460: return staffMembershipUser;
461: }
462:
463: ////////////////////////////////////////////////////////////////////////////
464:
465: /// <summary>
466: ///
467: /// </summary>
468: public static bool NullifyUserId(int staffId)
469: {
470: bool b;
471: Ia.Ngn.Cl.Model.Staff staff;
472:
473: using (var db = new Ia.Ngn.Cl.Model.Ngn())
474: {
475: staff = (from s in db.Staffs where s.Id == staffId select s).SingleOrDefault();
476:
477: staff.UserId = Guid.Empty;
478:
479: db.Staffs.Attach(staff);
480: db.Entry(staff).Property(u => u.UserId).IsModified = true;
481:
482: db.SaveChanges();
483:
484: b = true;
485:
486: staffList = null;
487: }
488:
489: return b;
490: }
491:
492: ////////////////////////////////////////////////////////////////////////////
493:
494: /// <summary>
495: ///
496: /// </summary>
497: public static void UnlockoutAllMembershipUsers()
498: {
499: using (var db = new Ia.Ngn.Cl.Model.Ngn())
500: {
501: db.Database.ExecuteSqlRaw("update Memberships set IsLockedOut = 0;");
502: }
503: }
504:
505: ////////////////////////////////////////////////////////////////////////////
506:
507: /// <summary>
508: ///
509: /// </summary>
510: public static void SetMembershipIsApprovedToFalseIfUserLastActivityDateIsLessThanDateTime(DateTime dateTime)
511: {
512: using (var db = new Ia.Ngn.Cl.Model.Ngn())
513: {
514: 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") + "'); ");
515: }
516: }
517:
518: ////////////////////////////////////////////////////////////////////////////
519:
520: /// <summary>
521: ///
522: /// </summary>
523: public static void DeleteStaffIfUserLastActivityDateIsLessThanDateTime(DateTime dateTime)
524: {
525: using (var db = new Ia.Ngn.Cl.Model.Ngn())
526: {
527: db.Database.ExecuteSqlRaw("delete Staffs where UserId in (select u.UserId from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
528: db.Database.ExecuteSqlRaw("delete Memberships where UserId in (select u.UserId from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
529: db.Database.ExecuteSqlRaw("delete UsersInRoles where UserId in (select u.UserId from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
530: db.Database.ExecuteSqlRaw("delete Profiles where UserId in (select u.UserId from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
531: db.Database.ExecuteSqlRaw("delete Users where UserId in (select u.UserId from Users u where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
532: }
533: }
534:
535: ////////////////////////////////////////////////////////////////////////////
536:
537: /// <summary>
538: ///
539: /// </summary>
540: public static void RemoveInactiveStaff(DateTime dateTime)
541: {
542: using (var db = new Ia.Ngn.Cl.Model.Ngn())
543: {
544: var v = Membership.GetAllUsers();
545: var list = Ia.Cl.Model.Identity.InactiveSinceDateTimeUserList(dateTime);
546:
547: //db.Database.ExecuteSqlRaw("update Memberships set IsLockedOut = 0;");
548: }
549: }
550:
551: ////////////////////////////////////////////////////////////////////////////
552: ////////////////////////////////////////////////////////////////////////////
553:
554: /// <summary>
555: ///
556: /// </summary>
557: public static Dictionary<string, string> StaffIpDictionary
558: {
559: get
560: {
561: var now = DateTime.UtcNow.AddHours(3);
562:
563: if (now.AddDays(-7) > staffIpDictionaryLastClearingTime)
564: {
565: staffIpDictionary.Clear();
566: staffIpDictionaryLastClearingTime = now;
567: }
568:
569: return staffIpDictionary;
570: }
571: }
572:
573: ////////////////////////////////////////////////////////////////////////////
574: ////////////////////////////////////////////////////////////////////////////
575: }
576:
577: ////////////////////////////////////////////////////////////////////////////
578: ////////////////////////////////////////////////////////////////////////////
579: }