Esempio n. 1
0
        /// <summary>
        /// Adds multiple users to a role
        /// </summary>
        /// <param name="role">The role to add the claim</param>
        /// <param name="userIds">The list of userIds to add to the role</param>
        /// <returns>IdentityResult</returns>
        public async Task <IdentityResult> AddUsersAsync(Role role, IEnumerable <string> userIds)
        {
            Ensure.Argument.NotNull(role, "role");
            Ensure.Argument.NotNull(userIds, "userIds");

            IdentityResult result;

            try
            {
                foreach (var userId in userIds)
                {
                    _db.UserRoles.Add(new UserRole()
                    {
                        RoleId = role.Id,
                        UserId = userId,
                    });
                }
                await _db.SaveChangesAsync();

                result = IdentityResult.Success;
            }
            catch (Exception ex)
            {
                result = IdentityResult.Failed(new IdentityError()
                {
                    Description = ex.Message
                });
            }

            return(result);
        }
Esempio n. 2
0
        public async Task <Tenant> CreateAsync(string tenantKey, string tenantName)
        {
            if (await KeyExistsAsync(tenantKey))
            {
                throw new Exception($"Cannot create tenant {tenantName}. Tenant key {tenantKey} already exists.");
            }

            var tenant = new Tenant()
            {
                Id        = KeyGen.NewGuid(),
                Key       = tenantKey,
                Name      = tenantName,
                OidcTitle = tenantName
            };

            _identityDb.Tenants.Add(tenant);
            await _identityDb.SaveChangesAsync();

            return(tenant);
        }
Esempio n. 3
0
        /// <summary>
        /// Adds a custom claim to a User
        /// </summary>
        /// <param name="role">The user to add the claim</param>
        /// <param name="claim">The new claim</param>
        /// <returns>IdentityResult</returns>
        public async Task <IdentityResult> AddClaimAsync(string userId, Claim claim, string claimScope = null)
        {
            Ensure.Argument.NotNull(userId);
            Ensure.Argument.NotNull(claim);

            IdentityResult result;

            try
            {
                var existingClaim = await _db.UserClaims.FirstOrDefaultAsync(x =>
                                                                             x.ClaimType == claim.Type &&
                                                                             x.ClaimValue == claim.Value &&
                                                                             x.UserId == userId
                                                                             );

                if (existingClaim == null)
                {
                    _db.UserClaims.Add(new UserClaim()
                    {
                        UserId     = userId,
                        ClaimType  = claim.Type,
                        ClaimValue = claim.Value,
                        ClaimScope = claimScope
                    });

                    await _db.SaveChangesAsync();
                }

                result = IdentityResult.Success;
            }
            catch (Exception ex)
            {
                result = IdentityResult.Failed(new IdentityError()
                {
                    Description = ex.Message
                });
            }

            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a top level security pool for a tenant
        /// </summary>
        public async Task <SecurityPool> CreateTenantPoolAsync(string tenantKey, string name, IEnumerable <Directory> directoriesToMap)
        {
            Ensure.Argument.NotNullOrEmpty(name);
            Ensure.Argument.NotNullOrEmpty(tenantKey);

            var tenant = await _db.Tenants.FirstOrDefaultAsync(x => x.Key == tenantKey);

            if (tenant == null)
            {
                throw new NullReferenceException($"Tenant {tenantKey} does not exist.");
            }


            var pool = new SecurityPool
            {
                PoolType = PoolType.Client,
                PoolId   = KeyGen.NewGuid(),
                TenantId = tenant.Id,
                Name     = name
            };

            if (directoriesToMap != null)
            {
                pool.DirectoryMap = directoriesToMap.Select(
                    d => new DirectoryMap
                {
                    DirectoryId = d.Id,
                    PoolId      = pool.PoolId
                }
                    ).ToList();
            }

            _db.SecurityPools.Add(pool);
            await _db.SaveChangesAsync();

            return(pool);
        }