Ejemplo n.º 1
0
        public async Task <ActionResult <LinkViewModel> > Update(
            [FromRoute] Guid id,
            [FromBody] LinkUpdateModel newLink)
        {
            var link = await database.GetById <LinkModel>(id);

            if (link == null)
            {
                return(NotFound());
            }

            if (!Can(Permissions.DELETE_LINKS, link))
            {
                return(NotFound());
            }

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

            if (!string.IsNullOrEmpty(newLink.Ident) && newLink.Ident != link.Ident)
            {
                if (!await ValidateIdent(newLink.Ident))
                {
                    return(BadRequest(new ResponseErrorModel("ident already exists")));
                }
                link.Ident = newLink.Ident;
            }

            if (!string.IsNullOrEmpty(newLink.Password))
            {
                link.PasswordHash = await hasher.GetEncodedHash(newLink.Password);
            }

            link.Enabled           = newLink.Enabled;
            link.PermanentRedirect = newLink.PermanentRedirect;
            link.TotalAccessLimit  = newLink.TotalAccessLimit;
            link.Expires           = newLink.Expires;

            database.Update(link);
            await database.Commit();

            await cache.Remove <LinkModel>(link.Ident);

            return(Ok(new LinkViewModel(link, AuthorizedUser)));
        }
Ejemplo n.º 2
0
        public async Task ValidateDestinationTest()
        {
            Assert.ThrowsAsync <ArgumentNullException>(
                () => LinksUtil.ValidateDestination(null));

            foreach (var link in ValidLinks)
            {
                Assert.IsTrue(
                    await LinksUtil.ValidateDestination(link),
                    $"Validation of (valid) link '{link}' failed");
            }

            foreach (var link in InvalidLinks)
            {
                Assert.IsFalse(
                    await LinksUtil.ValidateDestination(link),
                    $"Validation of (invalid) link '{link}' failed");
            }
        }
Ejemplo n.º 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)));
        }