Exemplo n.º 1
0
 public void Delete(int id)
 {
     using (var db = new CompAgriConnection())
     {
         var user = db.User.FirstOrDefault(u => u.User_Id == id);
         db.User.Remove(user);
         db.SaveChanges();
     }
 }
Exemplo n.º 2
0
        public UserDto Post([FromBody] UserDto user)
        {
            User userFromDatabase = null;

            using (var db = new CompAgriConnection())
            {
                if (user.Email == null && user.UserName == null)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                if (user.Email != null)
                {
                    userFromDatabase = db.User.FirstOrDefault(u => u.Email == user.Email);
                }

                if (userFromDatabase == null && user.UserName != null)
                {
                    userFromDatabase = db.User.FirstOrDefault(u => u.UserName == user.UserName);
                }

                if (userFromDatabase == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                var hashedPassword = PasswordUtils.HashPassword(user.Password, userFromDatabase.PasswordSalt);

                if (hashedPassword != userFromDatabase.Password)
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }
                else
                {
                    userFromDatabase.Token = TokenUtils.GenerateToken();
                    db.SaveChanges();
                    LogUserLogged(userFromDatabase);

                    var userToSend = new UserDto(userFromDatabase, true);
                    return(userToSend);
                }
            }
        }
Exemplo n.º 3
0
        public void Delete()
        {
            string token = UserUtils.GetUserToken(Request);

            if (token == null)
            {
                return;
            }

            using (var db = new CompAgriConnection())
            {
                var user = db.User.FirstOrDefault(u => u.Token == token);
                if (user == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                user.Token = null;
                db.SaveChanges();
            }
        }
Exemplo n.º 4
0
        public UserDto Post([FromBody] UserDto userDto)
        {
            if (!userDto.IsValid())
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            using (var db = new CompAgriConnection())
            {
                var previousUser = db.User.FirstOrDefault(u => (u.UserName != null && u.UserName == userDto.UserName) || (u.Email != null && u.Email == userDto.Email));
                if (previousUser != null)
                {
                    throw WebExceptionsFactory.GetUserDuplicatedException();
                }

                var userBeforeSave = userDto.User();
                userBeforeSave.PasswordSalt = PasswordUtils.GenerateSalt();
                userBeforeSave.Password     = PasswordUtils.HashPassword(userBeforeSave.Password, userBeforeSave.PasswordSalt);

                var user = db.User.Add(userBeforeSave);
                db.SaveChanges();
                return(new UserDto(user));
            }
        }