コード例 #1
0
        public void Delete(long id)
        {
            var item = context.Set <Mail>()
                       .Where(x => x.UserId == user.Id && x.Id == id)
                       .First();

            context.Remove(item);
            context.SaveChanges();
        }
コード例 #2
0
        public async Task <IActionResult> Delete(Guid uid)
        {
            var file = context.Set <File>()
                       .Single(x => x.UserId == user.Id && x.UID == uid);

            System.IO.File.Delete(file.Path);
            context.Remove(file);
            await context.SaveChangesAsync();

            return(Ok());
        }
コード例 #3
0
 public TemplateDto Get(long id)
 {
     return(context.Set <Template>()
            .Where(x => x.Id == id && user.Id == x.User.Id)
            .OrderByDescending(x => x.Id)
            .ToList()
            .Select(x => new TemplateDto
     {
         Id = x.Id,
         Content = x.Content,
         Description = x.Description,
         IsHtml = x.IsHtml,
         Name = x.Name,
         Subject = x.Subject,
         MailId = x.MailId
     })
            .First());
 }
コード例 #4
0
        public User GetAuthenticatedUser()
        {
            var tokenId = httpContextAccessor?.HttpContext?.User?.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;

            if (tokenId != null)
            {
                var id = Convert.ToInt32(tokenId);
                return(context.Set <User>().FirstOrDefault(x => x.Id == id));
            }

            return(null);
        }
コード例 #5
0
        public IActionResult Login([FromBody] CredentialDto credential)
        {
            User user;

            try
            {
                credential.Password = credential.Password.ToSha512();
                user = context.Set <User>()
                       .Where(x => x.Email == credential.Email && x.Password == credential.Password)
                       .Select(x => new User
                {
                    Id    = x.Id,
                    Email = x.Email
                })
                       .First();
            }
            catch
            {
                throw new InvalidCredentialException();
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(configuration.GetValue <string>("JwtSecret"));
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Email, user.Email.ToString()),
                    new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                }),
                Expires            = DateTime.UtcNow.AddHours(2),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var securityToken = tokenHandler.CreateToken(tokenDescriptor);

            var token = new TokenDto
            {
                Token = tokenHandler.WriteToken(securityToken)
            };

            return(Ok(token));
        }
コード例 #6
0
        private async Task <Domain.Image> GetImageWitEditedAndValidate(Guid uid, Guid?token)
        {
            Domain.Image image = null;

            try
            {
                image = await context.Set <Domain.Image>()
                        .Where(x => x.IsPublic == !token.HasValue && x.UID == uid)
                        .Select(x => new Domain.Image
                {
                    User = new Backend.Domain.User
                    {
                        Token = x.User.Token
                    },
                    Path         = x.Path,
                    Width        = x.Width,
                    Height       = x.Height,
                    EditedImages = x.EditedImages,
                    MimeType     = x.MimeType,
                    Filename     = x.Filename,
                    Id           = x.Id
                })
                        .SingleAsync();
            }
            catch (Exception ex)
            {
                throw new Exceptions.FileNotFoundException(ex);
            }

            if (token.HasValue && image.User.Token != token.Value)
            {
                throw new Exceptions.FileNotFoundException(null);
            }

            return(image);
        }
コード例 #7
0
 public bool HasMenu(int id)
 {
     return(_context.Set <Menu>().Any(e => e.Id == id));
 }