示例#1
0
        public static Task CanAssign(AppContributors contributors, AssignContributor command, IUserResolver users, IAppLimitsPlan plan)
        {
            Guard.NotNull(command, nameof(command));

            return(Validate.It(() => "Cannot assign contributor.", async error =>
            {
                if (!command.Permission.IsEnumValue())
                {
                    error(new ValidationError("Permission is not valid.", nameof(command.Permission)));
                }

                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    error(new ValidationError("Contributor id not assigned.", nameof(command.ContributorId)));
                }
                else
                {
                    if (await users.FindByIdAsync(command.ContributorId) == null)
                    {
                        error(new ValidationError("Cannot find contributor id.", nameof(command.ContributorId)));
                    }
                    else if (contributors.TryGetValue(command.ContributorId, out var existing))
                    {
                        if (existing == command.Permission)
                        {
                            error(new ValidationError("Contributor has already this permission.", nameof(command.Permission)));
                        }
                    }
                    else if (plan.MaxContributors == contributors.Count)
                    {
                        error(new ValidationError("You have reached the maximum number of contributors for your plan."));
                    }
                }
            }));
        }
示例#2
0
        public static Task CanAssign(AppContributors contributors, Roles roles, AssignContributor command, IUserResolver users, IAppLimitsPlan plan)
        {
            Guard.NotNull(command, nameof(command));

            return(Validate.It(() => "Cannot assign contributor.", async e =>
            {
                if (!roles.ContainsKey(command.Role))
                {
                    e(Not.Valid("role"), nameof(command.Role));
                }

                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    e(Not.Defined("Contributor id"), nameof(command.ContributorId));
                }
                else
                {
                    var user = await users.FindByIdOrEmailAsync(command.ContributorId);

                    if (user == null)
                    {
                        throw new DomainObjectNotFoundException(command.ContributorId, "Contributors", typeof(IAppEntity));
                    }

                    command.ContributorId = user.Id;

                    if (!command.IsRestore)
                    {
                        if (string.Equals(command.ContributorId, command.Actor?.Identifier, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new DomainForbiddenException("You cannot change your own role.");
                        }

                        if (contributors.TryGetValue(command.ContributorId, out var role))
                        {
                            if (role == command.Role)
                            {
                                e(Not.New("Contributor", "role"), nameof(command.Role));
                            }
                        }
                        else
                        {
                            if (plan.MaxContributors > 0 && contributors.Count >= plan.MaxContributors)
                            {
                                e("You have reached the maximum number of contributors for your plan.");
                            }
                        }
                    }
                }
            }));
        }
示例#3
0
        public static Task CanAssign(AppContributors contributors, AssignContributor command, IUserResolver users, IAppLimitsPlan plan)
        {
            Guard.NotNull(command, nameof(command));

            return(Validate.It(() => "Cannot assign contributor.", async error =>
            {
                if (!command.Permission.IsEnumValue())
                {
                    error(new ValidationError("Permission is not valid.", nameof(command.Permission)));
                }

                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    error(new ValidationError("Contributor id not assigned.", nameof(command.ContributorId)));
                }
                else
                {
                    var user = await users.FindByIdOrEmailAsync(command.ContributorId);

                    if (user == null)
                    {
                        error(new ValidationError("Cannot find contributor id.", nameof(command.ContributorId)));
                    }
                    else
                    {
                        command.ContributorId = user.Id;

                        if (string.Equals(command.ContributorId, command.Actor?.Identifier, StringComparison.OrdinalIgnoreCase))
                        {
                            error(new ValidationError("You cannot change your own permission."));
                        }
                        else if (contributors.TryGetValue(command.ContributorId, out var existing))
                        {
                            if (existing == command.Permission)
                            {
                                error(new ValidationError("Contributor has already this permission.", nameof(command.Permission)));
                            }
                        }
                        else if (plan.MaxContributors == contributors.Count)
                        {
                            error(new ValidationError("You have reached the maximum number of contributors for your plan."));
                        }
                    }
                }
            }));
        }
示例#4
0
        public static Task CanAssign(AppContributors contributors, AssignContributor command, IUserResolver users, IAppLimitsPlan plan)
        {
            Guard.NotNull(command, nameof(command));

            return(Validate.It(() => "Cannot assign contributor.", async e =>
            {
                if (!command.Permission.IsEnumValue())
                {
                    e("Permission is not valid.", nameof(command.Permission));
                }

                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    e("Contributor id is required.", nameof(command.ContributorId));
                    return;
                }

                var user = await users.FindByIdOrEmailAsync(command.ContributorId);

                if (user == null)
                {
                    throw new DomainObjectNotFoundException(command.ContributorId, "Contributors", typeof(IAppEntity));
                }

                command.ContributorId = user.Id;

                if (string.Equals(command.ContributorId, command.Actor?.Identifier, StringComparison.OrdinalIgnoreCase) && !command.FromRestore)
                {
                    throw new SecurityException("You cannot change your own permission.");
                }

                if (contributors.TryGetValue(command.ContributorId, out var existing))
                {
                    if (existing == command.Permission)
                    {
                        e("Contributor has already this permission.", nameof(command.Permission));
                    }
                }
                else if (plan.MaxContributors == contributors.Count)
                {
                    e("You have reached the maximum number of contributors for your plan.");
                }
            }));
        }
        public static Task CanAssign(AppContributors contributors, Roles roles, AssignContributor command, IUserResolver users, IAppLimitsPlan plan)
        {
            Guard.NotNull(command, nameof(command));

            return(Validate.It(async e =>
            {
                if (!roles.Contains(command.Role))
                {
                    e(Not.Valid(nameof(command.Role)), nameof(command.Role));
                }

                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    e(Not.Defined(nameof(command.ContributorId)), nameof(command.ContributorId));
                }
                else
                {
                    var user = await users.FindByIdAsync(command.ContributorId);

                    if (user == null)
                    {
                        throw new DomainObjectNotFoundException(command.ContributorId);
                    }

                    if (!command.Restoring)
                    {
                        if (string.Equals(command.ContributorId, command.Actor?.Identifier, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new DomainForbiddenException(T.Get("apps.contributors.cannotChangeYourself"));
                        }

                        if (!contributors.TryGetValue(command.ContributorId, out _))
                        {
                            if (plan.MaxContributors > 0 && contributors.Count >= plan.MaxContributors)
                            {
                                e(T.Get("apps.contributors.maxReached"));
                            }
                        }
                    }
                }
            }));
        }