예제 #1
0
 public void FromSource(AppContributors source)
 {
     foreach (var(userId, role) in source)
     {
         Add(userId, role);
     }
 }
예제 #2
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."));
                    }
                }
            }));
        }
예제 #3
0
        public static void CanDelete(Roles roles, DeleteRole command, AppContributors contributors, AppClients clients)
        {
            Guard.NotNull(command, nameof(command));

            GetRoleOrThrow(roles, command.Name);

            Validate.It(() => "Cannot delete role.", e =>
            {
                if (string.IsNullOrWhiteSpace(command.Name))
                {
                    e(Not.Defined("Name"), nameof(command.Name));
                }
                else if (Role.IsDefaultRole(command.Name))
                {
                    e("Cannot delete a default role.");
                }

                if (clients.Values.Any(x => string.Equals(x.Role, command.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    e("Cannot remove a role when a client is assigned.");
                }

                if (contributors.Values.Any(x => string.Equals(x, command.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    e("Cannot remove a role when a contributor is assigned.");
                }
            });
        }
예제 #4
0
        public static void CanDelete(Roles roles, DeleteRole command, AppContributors contributors, AppClients clients)
        {
            Guard.NotNull(command, nameof(command));

            CheckRoleExists(roles, command.Name);

            Validate.It(e =>
            {
                if (string.IsNullOrWhiteSpace(command.Name))
                {
                    e(Not.Defined(nameof(command.Name)), nameof(command.Name));
                }
                else if (Roles.IsDefault(command.Name))
                {
                    e(T.Get("apps.roles.defaultRoleNotRemovable"));
                }

                if (clients.Values.Any(x => string.Equals(x.Role, command.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    e(T.Get("apps.roles.usedRoleByClientsNotRemovable"));
                }

                if (contributors.Values.Any(x => string.Equals(x, command.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    e(T.Get("apps.roles.usedRoleByContributorsNotRemovable"));
                }
            });
        }
        private IAppEntity App(AppContributors contributors)
        {
            var app = A.Fake <IAppEntity>();

            A.CallTo(() => app.Contributors)
            .Returns(contributors);
            A.CallTo(() => app.Roles)
            .Returns(roles);

            return(app);
        }
예제 #6
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.");
                            }
                        }
                    }
                }
            }));
        }
예제 #7
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."));
                        }
                    }
                }
            }));
        }
예제 #8
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.");
                }
            }));
        }
예제 #9
0
        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"));
                            }
                        }
                    }
                }
            }));
        }
예제 #10
0
        public static void CanRemove(AppContributors contributors, RemoveContributor command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(e =>
            {
                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    e(Not.Defined(nameof(command.ContributorId)), nameof(command.ContributorId));
                }

                var ownerIds = contributors.Where(x => x.Value == Role.Owner).Select(x => x.Key).ToList();

                if (ownerIds.Count == 1 && ownerIds.Contains(command.ContributorId))
                {
                    e(T.Get("apps.contributors.onlyOneOwner"));
                }
            });

            if (!contributors.ContainsKey(command.ContributorId))
            {
                throw new DomainObjectNotFoundException(command.ContributorId);
            }
        }
예제 #11
0
        public static void CanRemove(AppContributors contributors, RemoveContributor command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot remove contributor.", error =>
            {
                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    error(new ValidationError("Contributor id not assigned.", nameof(command.ContributorId)));
                }

                var ownerIds = contributors.Where(x => x.Value == AppContributorPermission.Owner).Select(x => x.Key).ToList();

                if (ownerIds.Count == 1 && ownerIds.Contains(command.ContributorId))
                {
                    error(new ValidationError("Cannot remove the only owner.", nameof(command.ContributorId)));
                }
            });

            if (!contributors.ContainsKey(command.ContributorId))
            {
                throw new DomainObjectNotFoundException(command.ContributorId, "Contributors", typeof(AppDomainObject));
            }
        }
예제 #12
0
        public static void CanRemove(AppContributors contributors, RemoveContributor command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot remove contributor.", e =>
            {
                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    e(Not.Defined("Contributor id"), nameof(command.ContributorId));
                }

                var ownerIds = contributors.Where(x => x.Value == Role.Owner).Select(x => x.Key).ToList();

                if (ownerIds.Count == 1 && ownerIds.Contains(command.ContributorId))
                {
                    e("Cannot remove the only owner.");
                }
            });

            if (!contributors.ContainsKey(command.ContributorId))
            {
                throw new DomainObjectNotFoundException(command.ContributorId, "Contributors", typeof(IAppEntity));
            }
        }
예제 #13
0
 public static AppContributors Apply(this AppContributors contributors, AppContributorAssigned @event)
 {
     return(contributors.Assign(@event.ContributorId, @event.Permission));
 }
예제 #14
0
 public static void Apply(this AppContributors contributors, AppContributorRemoved @event)
 {
     contributors.Remove(@event.ContributorId);
 }
예제 #15
0
 public static void Apply(this AppContributors contributors, AppContributorAssigned @event)
 {
     contributors.Assign(@event.ContributorId, @event.Permission);
 }
            public void HandleCommand(CreateApp command)
            {
                version++;

                contributors = contributors.Assign(command.Actor.Identifier, Role.Developer);
            }
            public void HandleCommand(AssignContributor command)
            {
                version++;

                contributors = contributors.Assign(command.ContributorId, Role.Developer);
            }
예제 #18
0
 public static AppContributors Apply(this AppContributors contributors, AppContributorRemoved @event)
 {
     return(contributors.Remove(@event.ContributorId));
 }