Example #1
0
        //Create [email protected] with password=Admin@12345 in the Admin role
        public static void InitializeIdentity(IlevusIdentityContext db)
        {
            var userManager = new IlevusUserManager(new IlevusUserStore(db));
            var roleManager = new IlevusRoleManager(new IlevusRoleStore(db));

            const string email    = "*****@*****.**";
            const string password = "******";

            const string roleName = "SysAdmin";

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

            if (role == null)
            {
                role = new IlevusRole(roleName)
                {
                    Description = "Todas as permissões do sistema."
                };
                var roleresult = roleManager.Create(role);
            }

            var user = userManager.FindByName(email);

            if (user == null)
            {
                user = new IlevusUser
                {
                    UserName       = email,
                    Email          = email,
                    EmailConfirmed = true,
                    Name           = "Administrador",
                    Surname        = "do Sistema",
                    Sex            = "M",
                    PhoneNumber    = "031 999999999",
                    Image          = null,

                    Professional = new UserProfessionalEntity()
                    {
                        City    = "Belo Horizonte",
                        County  = "Minas Gerais",
                        Country = "Brasil",
                        Address = "Sede da Ilevus",
                    }
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role Admin if not already added
            var rolesForUser = userManager.GetRoles(user.Id);

            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }


            const string userEmail    = "*****@*****.**";
            const string userPassword = "******";

            const string userRoleName = "User";

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

            if (userRole == null)
            {
                userRole = new IlevusRole(userRoleName)
                {
                    Description = "Usuário comum do sistema."
                };
                roleManager.Create(userRole);
            }

            var commonUser = userManager.FindByName(userEmail);

            if (commonUser == null)
            {
                commonUser = new IlevusUser
                {
                    UserName       = userEmail,
                    Email          = userEmail,
                    EmailConfirmed = true,
                    Name           = "Usuário",
                    Surname        = "Exemplo",
                    Sex            = "M",
                    PhoneNumber    = "031 988888888",
                    Image          = null,

                    Professional = new UserProfessionalEntity()
                    {
                        City    = "Belo Horizonte",
                        County  = "Minas Gerais",
                        Country = "Brasil",
                        Address = "Sede da Ilevus",
                    }
                };
                var result = userManager.Create(commonUser, userPassword);
                result = userManager.SetLockoutEnabled(commonUser.Id, false);
            }

            // Add user admin to Role Admin if not already added
            var rolesForCommonUser = userManager.GetRoles(commonUser.Id);

            if (!rolesForCommonUser.Contains(userRole.Name))
            {
                var result = userManager.AddToRole(commonUser.Id, userRole.Name);
            }
        }
Example #2
0
 public static void Initialize(IlevusIdentityContext db)
 {
     IndexChecks.EnsureUniqueIndexOnEmail(db.Users);
     IndexChecks.EnsureUniqueIndexOnUserName(db.Users);
     IndexChecks.EnsureUniqueIndexOnRoleName(db.Roles);
 }