예제 #1
0
        public IActionResult Post([FromBody] RegisterUserModel model)
        {
            Guard.AgainstNull(model, nameof(model));

            var registeredBy = "system";
            var result       = Request.GetSessionToken();
            var ok           = false;

            if (result.Ok)
            {
                using (_databaseContextFactory.Create())
                {
                    var session = _sessionRepository.Get(result.SessionToken);

                    registeredBy = session.Username;

                    ok = session.HasPermission(SystemPermissions.Manage.Users);
                }
            }
            else
            {
                int count;

                using (_databaseContextFactory.Create())
                {
                    count = _systemUserQuery.Count();
                }

                if (count == 0)
                {
                    if (_authorizationService is IAnonymousPermissions anonymousPermissions)
                    {
                        ok = anonymousPermissions.AnonymousPermissions()
                             .Any(item => item.Equals(SystemPermissions.Manage.Users));
                    }
                }
            }

            if (!ok)
            {
                return(Unauthorized());
            }

            _bus.Send(new RegisterUserCommand
            {
                Username     = model.Username,
                PasswordHash = _hashingService.Sha256(model.Password),
                RegisteredBy = registeredBy
            });

            return(Ok());
        }
예제 #2
0
        public void ProcessMessage(IHandlerContext <RegisterUserCommand> context)
        {
            var message = context.Message;

            if (string.IsNullOrEmpty(message.Username))
            {
                return;
            }

            if (string.IsNullOrEmpty(message.RegisteredBy))
            {
                return;
            }

            var id = Guid.NewGuid();

            using (_databaseContextFactory.Create())
            {
                var key = User.Key(message.Username);

                if (_keyStore.Contains(key))
                {
                    return;
                }

                var count = _systemUserQuery.Count();

                _keyStore.Add(id, key);

                var user   = new User(id);
                var stream = _eventStore.CreateEventStream(id);

                var registered = user.Register(message.Username, message.PasswordHash, message.RegisteredBy);

                if (count == 0)
                {
                    stream.AddEvent(user.AddRole("administrator"));
                }

                stream.AddEvent(registered);

                _eventStore.Save(stream);
            }
        }
예제 #3
0
        public IEnumerable <string> AnonymousPermissions()
        {
            var result = new List <string>();
            var count  = 0;

            using (_databaseContextFactory.Create(_configuration.ProviderName, _configuration.ConnectionString))
            {
                count = _systemUserQuery.Count();
                result.AddRange(_systemRoleQuery.Permissions("Anonymous"));
            }

            if (count == 0)
            {
                result.Add(SystemPermissions.Manage.Users);
                result.Add(SystemPermissions.Register.UserRequired);
            }

            return(result);
        }
예제 #4
0
        public IEnumerable <string> AnonymousPermissions()
        {
            int count;
            var result = new List <string>();

            using (_databaseContextFactory.Create())
            {
                count = _systemUserQuery.Count();
            }

            result.AddRange(_systemRoleQuery.Permissions("Anonymous"));

            if (count == 0)
            {
                result.Add(SystemPermissions.States.UserRequired);
                result.Add(SystemPermissions.Register.User);
            }

            return(result);
        }