/// <summary>
        /// Adds a user to the system, only if they do not exist.
        /// </summary>
        private static void AddInitialUser(this IDbAppContext context, User initialUser)
        {
            User user = context.GetUserBySmUserId(initialUser.SmUserId);

            if (user != null)
            {
                return;
            }

            user           = new User();
            user.Active    = true;
            user.Email     = initialUser.Email;
            user.GivenName = initialUser.GivenName;
            user.Initials  = initialUser.Initials;
            user.SmAuthorizationDirectory = initialUser.SmAuthorizationDirectory;
            user.SmUserId = initialUser.SmUserId;
            user.Surname  = initialUser.Surname;

            District district = null;

            if (initialUser.District != null)
            {
                district = context.GetDistrictByMinistryDistrictId(initialUser.District.MinistryDistrictID);
            }

            user.District = district;

            context.Users.Add(user);
            context.SaveChanges();

            string[] userRoles = initialUser.UserRoles.Select(x => x.Role.Name).ToArray();
            if (user.UserRoles == null)
            {
                user.UserRoles = new List <UserRole>();
            }

            foreach (string userRole in userRoles)
            {
                Role role = context.GetRole(userRole);
                if (role != null)
                {
                    user.UserRoles.Add(
                        new UserRole
                    {
                        EffectiveDate = DateTime.UtcNow.Date,
                        Role          = role
                    });
                }
            }

            context.Users.Update(user);
            context.SaveChanges();
        }
        private static void AddInitialServiceArea(this IDbAppContext context, ServiceArea initialServiceArea)
        {
            ServiceArea serviceArea = context.GetServiceAreaByMinistryServiceAreaId(initialServiceArea.MinistryServiceAreaID);

            if (serviceArea != null)
            {
                return;
            }

            serviceArea = new ServiceArea
            {
                MinistryServiceAreaID = initialServiceArea.MinistryServiceAreaID,
                Name      = initialServiceArea.Name,
                StartDate = initialServiceArea.StartDate
            };

            if (initialServiceArea.District != null)
            {
                District district = context.GetDistrictByMinistryDistrictId(initialServiceArea.District.MinistryDistrictID);
                serviceArea.District = district;
            }
            else
            {
                serviceArea.District = null;
            }

            context.ServiceAreas.Add(serviceArea);
            context.SaveChanges();
        }
        /// <summary>
        /// Adds a district to the system, only if it does not exist.
        /// </summary>
        private static void AddInitialDistrict(this IDbAppContext context, District initialDistrict)
        {
            District district = context.GetDistrictByMinistryDistrictId(initialDistrict.MinistryDistrictID);

            if (district != null)
            {
                return;
            }

            district = new District
            {
                MinistryDistrictID = initialDistrict.MinistryDistrictID,
                Name      = initialDistrict.Name,
                StartDate = initialDistrict.StartDate
            };

            if (initialDistrict.Region != null)
            {
                Region region = context.GetRegionByMinistryRegionId(initialDistrict.Region.MinistryRegionID);
                district.Region = region;
            }
            else
            {
                district.Region = null;
            }

            context.Districts.Add(district);
            context.SaveChanges();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds a region to the system, only if it does not exist.
        /// </summary>
        private static void AddInitialRegion(this IDbAppContext context, Region initialRegion)
        {
            Region region = context.GetRegionByMinistryRegionId(initialRegion.MinistryRegionID);

            if (region != null)
            {
                return;
            }

            region = new Region();
            region.MinistryRegionID = initialRegion.MinistryRegionID;
            region.Name             = initialRegion.Name;
            region.StartDate        = initialRegion.StartDate;

            context.Regions.Add(region);
            context.SaveChanges();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds a CCWJurisdiction to the system, only if it does not exist.
        /// </summary>
        private static void AddInitialCCWJurisdiction(this IDbAppContext context, CCWJurisdiction initialCCWJurisdiction)
        {
            CCWJurisdiction CCWJurisdiction = context.GetCCWJurisdictionByCode(initialCCWJurisdiction.Code);

            if (CCWJurisdiction != null)
            {
                return;
            }

            CCWJurisdiction               = new CCWJurisdiction();
            CCWJurisdiction.ActiveFlag    = initialCCWJurisdiction.ActiveFlag;
            CCWJurisdiction.Code          = initialCCWJurisdiction.Code;
            CCWJurisdiction.Description   = initialCCWJurisdiction.Description;
            CCWJurisdiction.EffectiveDate = initialCCWJurisdiction.EffectiveDate;
            CCWJurisdiction.ExpiryDate    = initialCCWJurisdiction.ExpiryDate;

            context.CCWJurisdictions.Add(CCWJurisdiction);
            context.SaveChanges();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Load User from HETS database using their userId and guid
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userId"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static User LoadUser(this IDbAppContext context, string userId, string guid = null)
        {
            User user = null;

            if (!string.IsNullOrEmpty(guid))
            {
                user = context.GetUserByGuid(guid);
            }

            if (user == null)
            {
                user = context.GetUserBySmUserId(userId);
            }

            if (user == null)
            {
                return(null);
            }

            if (guid == null)
            {
                return(user);
            }

            if (string.IsNullOrEmpty(user.Guid))
            {
                // self register (write the users Guid to thd db)
                user.Guid = guid;
                context.SaveChanges();
            }
            else if (!user.Guid.Equals(guid, StringComparison.OrdinalIgnoreCase))
            {
                // invalid account - guid doesn't match user credential
                return(null);
            }

            return(user);
        }
Ejemplo n.º 7
0
        public static User LoadUser(this IDbAppContext context, string username, string guid = null)
        {
            User user = null;

            if (!string.IsNullOrEmpty(guid))
            {
                user = context.GetUserByGuid(guid);
            }

            if (user == null)
            {
                user = context.GetUserBySmUserId(username);
            }

            if (user == null)
            {
                return(null);
            }

            if (guid != null)
            {
                if (string.IsNullOrEmpty(user.Guid))
                {
                    // Self register ...
                    user.Guid = guid;
                    context.SaveChanges();
                }
                else if (!user.Guid.Equals(guid, StringComparison.OrdinalIgnoreCase))
                {
                    // Registered users are not allowed to change their SiteMinder IDs ...
                    return(null);
                }
            }

            return(user);
        }
        private static void AddInitialUser(this IDbAppContext context, User initialUser)
        {
            User user = context.GetUserBySmUserId(initialUser.SmUserId);

            if (user != null)
            {
                return;
            }

            user = new User
            {
                Active    = true,
                Email     = initialUser.Email,
                GivenName = initialUser.GivenName,
                Initials  = initialUser.Initials,
                SmAuthorizationDirectory = initialUser.SmAuthorizationDirectory,
                SmUserId = initialUser.SmUserId,
                Surname  = initialUser.Surname
            };


            District district = null;

            if (initialUser.District != null)
            {
                district = context.GetDistrictByMinistryDistrictId(initialUser.District.MinistryDistrictID);
            }

            user.District = district;

            context.Users.Add(user);
            context.SaveChanges();

            string[] userRoles = initialUser.UserRoles.Select(x => x.Role.Name).ToArray();

            if (user.UserRoles == null)
            {
                user.UserRoles = new List <UserRole>();
            }

            foreach (string userRole in userRoles)
            {
                Role role = context.GetRole(userRole);
                if (role != null)
                {
                    user.UserRoles.Add(
                        new UserRole
                    {
                        EffectiveDate = DateTime.UtcNow,
                        Role          = role
                    });
                }
            }

            string[] userGroups = initialUser.GroupMemberships.Select(x => x.Group.Name).ToArray();
            if (user.GroupMemberships == null)
            {
                user.GroupMemberships = new List <GroupMembership>();
            }

            foreach (string userGroup in userGroups)
            {
                Group group = context.GetGroup(userGroup);
                if (group != null)
                {
                    user.GroupMemberships.Add(
                        new GroupMembership
                    {
                        Active = true,
                        Group  = context.GetGroup("Other")
                    });
                }
            }

            context.Users.Update(user);
            context.SaveChanges();
        }