Example #1
0
        //Create [email protected] with password=Admin@123456 in the Admin role
        public static void InitializeIdentityForEF(ApplicationDbContext db)
        {
            var          userManager = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var          roleManager = HttpContext.Current.GetOwinContext().Get <ApplicationRoleManager>();
            const string name        = "*****@*****.**";
            const string password    = "******";
            const string roleName    = "Admin";

            //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleName);

            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            var user = userManager.FindByName(name);

            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = name, Email = name, EmailConfirmed = true
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            var groupManager = new ApplicationGroupManager();
            var newGroup     = new ApplicationGroup("SuperAdmins", "Full Access to All");

            groupManager.CreateGroup(newGroup);
            groupManager.SetUserGroups(user.Id, new string[] { newGroup.Id });
            groupManager.SetGroupRoles(newGroup.Id, new string[] { role.Name });
        }
Example #2
0
        private void SetGroups()
        {
            var roleStore   = new RoleStore <Role, int, UserRole>(_context);
            var roleManager = new ApplicationRoleManager(roleStore);

            var userStore   = new UserStore <User, Role, int, UserLogin, UserRole, UserClaim>(_context);
            var userManager = new ApplicationUserManager(userStore);

            var groupManagerStore = new ApplicationGroupStore(_context);
            var groupManager      = new ApplicationGroupManager(groupManagerStore, roleManager, userManager);

            foreach (var group in GroupsToAdd)
            {
                var result = groupManager.CreateGroup(group);
                if (!result.Succeeded)
                {
                    throw new Exception(string.Join(",", result.Errors.ToArray()));
                }
                var roles = RolesToAdd.Select(x => x.Id).ToArray();
                var resultSetGroupRoles = groupManager.SetGroupRoles(group.Id, roles);
                if (!resultSetGroupRoles.Succeeded)
                {
                    throw new Exception(string.Join(",", resultSetGroupRoles.Errors.ToArray()));
                }
            }
        }
Example #3
0
        private void AddUserAuthentication(CRMEntities context)
        {
            var          userManager  = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var          roleManager  = HttpContext.Current.GetOwinContext().Get <ApplicationRoleManager>();
            const string name         = "*****@*****.**";
            const string FirstName    = "admin";
            const string LastName     = "System";
            const int    UserStatusID = 1;
            const string password     = "******";
            const string roleName     = "Admin";
            var          aRole        = new ApplicationRole(roleName);

            context.Roles.Add(aRole);
            context.SaveChanges();

            var user = new ApplicationUser {
                UserName = name, Email = name, EmailConfirmed = true, FirstName = FirstName, LastName = LastName, UserStatusID = UserStatusID
            };
            var result = userManager.Create(user, password);

            result = userManager.SetLockoutEnabled(user.Id, false);

            var groupManager = new ApplicationGroupManager();
            var newGroup     = new ApplicationGroup("SuperAdmins", "Full Access to All");

            groupManager.CreateGroup(newGroup);
            groupManager.SetUserGroups(user.Id, new string[] { newGroup.Id });
            groupManager.SetGroupRoles(newGroup.Id, new string[] { aRole.Name });
        }
Example #4
0
        private void InitializeIdentityForEf(ApplicationDbContext db)
        {
            const string name     = "*****@*****.**";
            const string password = "******";

            PasswordHasher  hasher    = new PasswordHasher();
            ApplicationUser adminUser = new ApplicationUser {
                UserName              = name,
                Email                 = name,
                PasswordHash          = hasher.HashPassword(password),
                LockoutEnabled        = false,
                EmailConfirmed        = true,
                IsActiveDirectoryUser = true
            };

            var adminUserResult = db.Users.FirstOrDefault <ApplicationUser>(x => x.UserName == adminUser.UserName);

            if (adminUserResult == null)
            {
                adminUserResult = db.Users.Add(adminUser);
                db.SaveChanges();
            }

            //Because of lazy loading of OwinContext, the method(FindByName) hange in diversity conditions then it's commented by me

            //db.Configuration.LazyLoadingEnabled = true;
            //var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
            //var user = userManager.FindByName(name);
            //if (user == null)
            //{
            //    user = new ApplicationUser
            //    {
            //        UserName = name,
            //        PasswordHash = hasher.HashPassword(password),
            //        Email = name,
            //        EmailConfirmed = true,
            //        IsActiveDirectoryUser = true
            //    };
            //    var result = userManager.Create(user, password);
            //    result = userManager.SetLockoutEnabled(user.Id, false);
            //}

            var groupManager = new ApplicationGroupManager();
            var newGroup     = new ApplicationGroup("SuperAdmin", "Full Access to All");

            if (!groupManager.Groups.Any(x => x.Name == newGroup.Name))
            {
                groupManager.CreateGroup(newGroup);
                groupManager.SetUserGroups(adminUserResult.Id, new int[] { newGroup.Id });
            }
            else
            {
                var group = groupManager.Groups.FirstOrDefault(x => x.Name == newGroup.Name);
                if (!group.ApplicationUsers.Any(x => x.ApplicationUserId == adminUserResult.Id))
                {
                    groupManager.SetUserGroups(adminUserResult.Id, new int[] { group.Id });
                }
            }
        }
Example #5
0
        public static void InitializeIdentityForEF(ApplicationDbContext db)
        {
            //var userManager = HttpContext.Current
            //    .GetOwinContext().GetUserManager<ApplicationUserManager>();
            //var roleManager = HttpContext.Current
            //    .GetOwinContext().Get<ApplicationRoleManager>();

            var roleStore   = new RoleStore <ApplicationRole, string, ApplicationUserRole>(db);
            var roleManager = new RoleManager <ApplicationRole, string>(roleStore);
            var userStore   = new UserStore <ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>(db);
            var userManager = new UserManager <ApplicationUser, string>(userStore);


            const string name     = "*****@*****.**";
            const string email    = "*****@*****.**";
            const string password = "******";
            const string roleName = "Admin";

            //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleName);

            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            var user = userManager.FindByName(name);

            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName       = name,
                    FirstName      = "Admin",
                    LastName       = "WorkCard.vn",
                    Email          = email,
                    EmailConfirmed = true
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
                userManager.AddToRole(user.Id, roleName);
            }

            var groupManager = new ApplicationGroupManager();
            var newGroup     = new ApplicationGroup("SuperAdmins", "Full Access to All");

            groupManager.CreateGroup(newGroup);
            groupManager.SetUserGroups(user.Id, new string[] { newGroup.Id });
            groupManager.SetGroupRoles(newGroup.Id, new string[] { role.Name });
        }
Example #6
0
        protected override void Seed(ApplicationDbContext context)
        {
            context.ApplicationActionPermissions.AddOrUpdate(new ApplicationActionPermission {
                ActionName = "Create", ControllerName = "ActionPermissionController", Id = 1
            });
            var roleManager = new ApplicationRoleManager(new ApplicationRoleStore(context));
            var userManager = new ApplicationUserManager(new ApplicationUserStore(context));

            const string name     = "*****@*****.**";
            const string password = "******";
            const string roleName = "Admin";

            //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleName);

            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            var user = userManager.FindByName(name);

            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = name, Email = name, EmailConfirmed = true
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            var groupManager = new ApplicationGroupManager(context, userManager, roleManager);
            var newGroup     = new ApplicationGroup("SuperAdmins", "Full Access to All")
            {
                IsAdmin = true
            };

            groupManager.CreateGroup(newGroup);
            groupManager.SetUserGroups(user.Id, new string[] { newGroup.Id });
            groupManager.SetGroupRoles(newGroup.Id, new string[] { role.Name });
        }
Example #7
0
        public static void InitializeIdentityForEF(ApplicationDbContext db)
        {
            var userManager = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var roleManager = HttpContext.Current.GetOwinContext().Get <ApplicationRoleManager>();

            //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleName);

            if (role == null)
            {
                role = new ApplicationRole(roleName, roleDesc);
                var roleresult = roleManager.Create(role);
                if (!roleresult.Succeeded)
                {
                    throw new Exception(roleresult.Errors.ElementAt(0));
                }

                var    groupManager = new ApplicationGroupManager();
                string groupName    = "SuperAdmin";
                string groupDesc    = "Super Admin Full Access to All";
                var    group        = groupManager.FindByName(groupName);
                if (group == null)
                {
                    var newGroup    = new ApplicationGroup(groupName, groupDesc);
                    var groupresult = groupManager.CreateGroup(newGroup);
                    if (!groupresult.Succeeded)
                    {
                        roleresult = roleManager.Delete(role);
                        if (!roleresult.Succeeded)
                        {
                            throw new Exception(roleresult.Errors.ElementAt(0));
                        }
                    }

                    var user      = userManager.FindByName(userName);
                    var emailUser = userManager.FindByEmail(emailName);
                    if (user == null && emailUser == null)
                    {
                        user = new ApplicationUser {
                            UserName = userName, Email = emailName, CreatedTime = DateTime.Now, CreatedBy = GlobalEnum.SystemName.System, RowStatus = GlobalEnum.RowStatus.Active
                        };
                        var userresult = userManager.Create(user, password);
                        userresult = userManager.SetLockoutEnabled(user.Id, false);
                        if (!userresult.Succeeded)
                        {
                            roleresult = roleManager.Delete(role);
                            if (!roleresult.Succeeded)
                            {
                                throw new Exception(roleresult.Errors.ElementAt(0));
                            }

                            groupresult = groupManager.DeleteGroup(group.Id);
                            if (!groupresult.Succeeded)
                            {
                                throw new Exception(groupresult.Errors.ElementAt(0));
                            }
                        }

                        groupManager.SetUserGroups(user.Id, new string[] { newGroup.Id });
                        groupManager.SetGroupRoles(newGroup.Id, new string[] { role.Name });
                    }
                }
            }
        }
        //Create [email protected] with password=Admin@123456 in the Admin role
        public static void InitializeIdentityForEF(ApplicationDbContext db)
        {
            var          userManager = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var          roleManager = HttpContext.Current.GetOwinContext().Get <ApplicationRoleManager>();
            const string name        = "*****@*****.**";
            const string password    = "******";
            const string roleName    = "Admin";

            //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleName);

            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            var user  = userManager.FindByName(name);
            var user3 = userManager.FindByName(name);

            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = name, Email = name, EmailConfirmed = true
                };
                user.FirstName  = "FAdmin";
                user.LastName   = "LAdmin";
                user.Department = "Information Technology";

                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);

                //Create other users
                string name2     = "*****@*****.**";
                string password2 = "Test!23";
                var    user2     = new ApplicationUser {
                    UserName = name2, Email = name2, EmailConfirmed = true
                };
                user2.FirstName  = "Robert";
                user2.LastName   = "Swift";
                user2.Department = "Information Technology";
                result           = userManager.Create(user2, password2);
                result           = userManager.SetLockoutEnabled(user2.Id, false);


                string name3     = "*****@*****.**";
                string password3 = "Test!23";
                user3 = new ApplicationUser {
                    UserName = name3, Email = name3, EmailConfirmed = true
                };
                user3.FirstName  = "Abhi";
                user3.LastName   = "Khaitan";
                user3.Department = "Information Technology";

                result = userManager.Create(user3, password3);
                result = userManager.SetLockoutEnabled(user3.Id, false);
            }

            var groupManager = new ApplicationGroupManager();
            var newGroup     = new ApplicationGroup("SuperAdmins", "Full Access to All");

            groupManager.CreateGroup(newGroup);
            groupManager.SetUserGroups(user.Id, new string[] { newGroup.Id });
            groupManager.SetGroupRoles(newGroup.Id, new string[] { role.Name });

            groupManager.SetUserGroups(user3.Id, new string[] { newGroup.Id });
            groupManager.SetGroupRoles(newGroup.Id, new string[] { role.Name });

            foreach (var u in db.Users)
            {
                HseqUser hseqUser = new HseqUser();
                hseqUser.UserID      = u.Id;
                hseqUser.Coordinator = true;
                hseqUser.Approver    = true;
                hseqUser.Assignee    = true;
                hseqUser.Owner       = true;

                db.HseqUsers.Add(hseqUser);
            }

            db.SaveChanges();
        }
Example #9
0
        public static void InitializeIdentityForEF(ApplicationDbContext db)
        {
            var          userManager = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var          roleManager = HttpContext.Current.GetOwinContext().Get <ApplicationRoleManager>();
            const string name        = "*****@*****.**";
            const string password    = "******";

            //Create Role Admin if it does not exist
            string[] roles =
            {
                "Admin", "Manager", "Leader", "IsDelete", "Approved", "GA", "ManageDepartmentShift"
            };

            foreach (string roleName in roles)
            {
                var role = roleManager.FindByName(roleName);
                if (role == null)
                {
                    role = new ApplicationRole(roleName, "Only " + roleName);
                    roleManager.Create(role);
                }
                if (roleName == "Admin")
                {
                    var newUser = new ApplicationUser()
                    {
                        UserName       = name,
                        Email          = name,
                        EmailConfirmed = true
                    };
                    var user = userManager.FindByName(name);
                    if (user == null)
                    {
                        userManager.Create(newUser, password);
                        userManager.SetLockoutEnabled(newUser.Id, false);
                        userManager.AddToRole(newUser.Id, roleName);
                    }
                    else
                    {
                        userManager.AddToRole(user.Id, roleName);
                    }
                    // Group Role
                    const string groupName    = "Super Admins";
                    var          groupManager = new ApplicationGroupManager();
                    var          newGroup     = new ApplicationGroup(groupName, "Full Access to All");

                    groupManager.CreateGroup(newGroup);
                    groupManager.SetUserGroups(newUser.Id, new string[] { newGroup.Id });
                    groupManager.SetGroupRoles(newGroup.Id, new string[] { role.Name });
                }
            }

            db.Departments.AddOrUpdate(
                d => d.DepartmentID,
                new Department()
            {
                DepartmentID = "IT", Name = "PD-IT", ParentID = "Root", Sort = 1, Description = "Dept PD-IT"
            },
                new Department()
            {
                DepartmentID = "EQ", Name = "EQ", ParentID = "Root", Sort = 2, Description = "Dept EQ"
            },
                new Department()
            {
                DepartmentID = "PL", Name = "PL", ParentID = "Root", Sort = 3, Description = "Dept PL"
            },
                new Department()
            {
                DepartmentID = "GA", Name = "GA", ParentID = "Root", Sort = 4, Description = "Dept GA"
            },
                new Department()
            {
                DepartmentID = "EDU", Name = "Edu", ParentID = "Root", Sort = 5, Description = "Dept Edu"
            },
                new Department()
            {
                DepartmentID = "ACC", Name = "ACC", ParentID = "Root", Sort = 6, Description = "Dept ACC"
            }
                );
        }