Exemplo n.º 1
0
        /// <summary>
        /// Seeding database with admin user and password admin. Also add role 'Admin'
        /// </summary>
        /// <param name="userService">Service to include user</param>
        public static void SeedUsers(IUserService userService)
        {
            User userAdmin = userService.FindByEmailAsync("*****@*****.**").Result;

            if (userAdmin == null)
            {
                User user = new User
                {
                    UserName     = "******",
                    Email        = "*****@*****.**",
                    FirstName    = "system",
                    LastName     = "admin",
                    IsSuperAdmin = true
                };
                userService.CreateAsync(user, "@dm1n1stR4tOr").Wait();
            }
            userAdmin = userService.FindByEmailAsync("*****@*****.**").Result;
            if (userAdmin != null)
            {
                if (!userService.GetRolesAsync(userAdmin).Result.Contains(RoleClaims.ADMIN_GROUP))
                {
                    userService.AddToRoleAsync(userAdmin, RoleClaims.ADMIN_GROUP).Wait();
                }
                IList <Claim> existingClaims = userService.GetClaimsAsync(userAdmin).Result;
                foreach (Claim claim in RoleClaims.GetAllAdminClaims())
                {
                    if (existingClaims.FirstOrDefault(x => claim.Type.Equals(x.Type) && claim.Value.Equals(x.Value)) == null)
                    {
                        userService.AddClaimAsync(userAdmin, claim).Wait();
                    }
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Erzeugen aller standart rollen (Admin und Datenschutzbeauftragter mit den jeweiligen rechten)
 /// </summary>
 /// <param name="roleService">rollen service</param>
 public static void SeedRoles(IRoleService roleService)
 {
     foreach (string roleToCreate in RoleClaims.DEFAULT_GROUPS)
     {
         Role role = roleService.FindRoleByNameAsync(roleToCreate).Result;
         if (role == null)
         {
             roleService.CreateAsync(new Role()
             {
                 Name = roleToCreate
             }).Wait();
         }
         role = roleService.FindRoleByNameAsync(roleToCreate).Result;
         if (role != null)
         {
             IList <Claim> existingClaims = roleService.GetClaimsAsync(role).Result;
             List <Claim>  claimsToCheck  = new List <Claim>();
             if (role.Name.Equals(RoleClaims.ADMIN_GROUP))
             {
                 claimsToCheck = RoleClaims.GetAllAdminClaims();
             }
             else if (role.Name.Equals(RoleClaims.DATA_SECURITY_ENGINEER_GROUP))
             {
                 claimsToCheck = RoleClaims.GetAllDsgvoClaims();
             }
             foreach (Claim claim in claimsToCheck)
             {
                 if (existingClaims.FirstOrDefault(x => x.Type.Equals(claim.Type) && x.Value.Equals(claim.Value)) == null)
                 {
                     roleService.AddClaimAsync(role, claim).Wait();
                 }
             }
         }
     }
 }
Exemplo n.º 3
0
 public async Task <IdentityResult> RemoveClaimAsync(Role role, Claim claim)
 {
     if ((role.Id == 1 && RoleClaims.GetAllAdminClaims().FirstOrDefault(x => x.Type.Equals(claim.Type)) != null &&
          RoleClaims.GetAllAdminClaims().FirstOrDefault(x => x.Value.Equals(claim.Value)) != null) ||
         role.Id == 2 && RoleClaims.GetAllDsgvoClaims().FirstOrDefault(x => x.Type.Equals(claim.Type)) != null &&
         RoleClaims.GetAllDsgvoClaims().FirstOrDefault(x => x.Value.Equals(claim.Value)) != null)
     {
         return(IdentityResult.Success);
     }
     return(await manager.RemoveClaimAsync(role, claim));
 }