Exemple #1
0
        //public override void Add(UserRole entity, string UserId = null) => throw new NotImplementedException();
        //public override void Remove(UserRole entity, string UserId = null) => throw new NotImplementedException();

        public async Task AddRolesAndClaims(User entity, string UserId = null, bool saveChanges = false)
        {
            if (entity == null || entity.UserRoles == null)
            {
                throw new ArgumentNullException("entity == null || entity.UserRoles == null");
            }

            foreach (var role in entity.UserRoles)
            {
                var resp = await CanAssignRole(role.RoleName);

                if (!resp.Success)
                {
                    throw new ApplicationException(resp.Message);
                }

                base.Add(role, UserId);
            }
            try
            {
                if (saveChanges)
                {
                    await _repository.SaveChangesAsync();
                }
                await _userClaimRepository.AddClaimsForRoles(entity, UserId, saveChanges);
            }
            catch (Exception ex)
            {
                throw;
            }
        }