Ejemplo n.º 1
0
        ///<inheritdoc/>
        public async Task <SimpleOperationResult> AddRoleAsync(Guid scopeId, Guid userId, IEnumerable <Guid> roles, CancellationToken cancellationToken)
        {
            bool success = false;
            IDictionary <string, string> errors = new Dictionary <string, string>();

            if (roles?.Count() > 0)
            {
                if (roles.Distinct().Count() == roles.Count())
                {
                    IList <Role> rolesList = new List <Role>();

                    foreach (Guid item in roles)
                    {
                        if (item != Guid.Empty)
                        {
                            Role role = await _roleDomain.GetByKeyAsync(item, cancellationToken);

                            if (role != null)
                            {
                                if (role.Scope.Id == scopeId)
                                {
                                    rolesList.Add(role);
                                }
                                else
                                {
                                    errors.Add(item.ToString(), _localizer["ROLE_NOT_BELONG_SCOPE"]);
                                }
                            }
                            else
                            {
                                errors.Add(item.ToString(), _localizer["ROLE_NOT_FOUND"]);
                            }
                        }
                        else
                        {
                            errors.Add(item.ToString(), _localizer["INVALID_ROLE"]);
                        }
                    }

                    if (errors.Count == 0)
                    {
                        return(await _dmn.AddRoleAsync(userId, rolesList, cancellationToken));
                    }
                }
                else
                {
                    errors.Add("Roles", _localizer["DUPLICATED_ROLE_LIST"]);
                }
            }
            else
            {
                errors.Add("Roles", _localizer["ROLE_LIST_REQUIRED"]);
            }

            return(new SimpleOperationResult(success, errors));
        }