Example #1
0
        private async Task InitializeRootUser()
        {
            logger.LogInformation("Initializing root user...");

            var userName = config.GetValue <string>(Constants.ConfigKeyInitializationUserName);

            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentException("root username not provided");
            }

            var password = config.GetValue <string>(Constants.ConfigKeyInitializationPassword);

            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException("root user password not provided");
            }

            var user = new UserModel
            {
                UserName     = userName,
                PasswordHash = await pwHasher.GetEncodedHash(password),
                Permissions  = Permissions.ADMINISTRATOR,
            };

            database.Create(user);
            await database.Commit();

            logger.LogInformation($"Root user with name '{userName}' and the provided password was created.");
        }
Example #2
0
        public async Task <ActionResult <IEnumerable <UserViewModel> > > Create([FromBody] UserCreateModel user)
        {
            if (!await UsernameAlreadyExists(user.UserName))
            {
                return(BadRequest(new ResponseErrorModel("username already exists")));
            }

            var newUser = new UserModel
            {
                UserName     = user.UserName,
                PasswordHash = await hasher.GetEncodedHash(user.Password),
                Permissions  = user.Permissions,
            };

            database.Create(newUser);
            await database.Commit();

            return(Created(
                       $"/api/users/{newUser.Guid}",
                       new UserViewModel(newUser)));
        }
Example #3
0
        public async Task <ActionResult <LinkViewModel> > Create([FromBody] LinkCreateModel link)
        {
            if (string.IsNullOrEmpty(link.Ident))
            {
                do
                {
                    link.Ident = RandomUtil.GetString(Constants.RandomIdentLength, Constants.RandomIdentChars);
                }while (!await ValidateIdent(link.Ident));
            }
            else if (!await ValidateIdent(link.Ident))
            {
                return(BadRequest(new ResponseErrorModel("ident already exists")));
            }

            if (!await LinksUtil.ValidateDestination(link.Destination))
            {
                return(BadRequest(new ResponseErrorModel("invalid destination url")));
            }

            var newLink = new LinkModel
            {
                Creator      = AuthorizedUser,
                Destination  = link.Destination,
                Enabled      = link.Enabled,
                Expires      = link.Expires,
                Ident        = link.Ident,
                PasswordHash = string.IsNullOrEmpty(link.Password)
                    ? null
                    : await hasher.GetEncodedHash(link.Password),
                PermanentRedirect = link.PermanentRedirect,
                TotalAccessLimit  = link.TotalAccessLimit,
            };

            database.Create(newLink);
            await database.Commit();

            return(Created(
                       $"/api/links/{newLink.Guid}",
                       new LinkViewModel(newLink, AuthorizedUser)));
        }