Exemple #1
0
        public VarlikResult <string> ForgotPassword(string mail)
        {
            var result = new VarlikResult <string>();

            using (var ctx = new VarlikContext())
            {
                var user = ctx.User.FirstOrDefault(l => l.Mail == mail && !l.IsDeleted);
                if (user != null)
                {
                    user.Password = Guid.NewGuid().ToString();
                    user.Password = VarlikHasher.Hash(user.Password);
                    try
                    {
                        ctx.SaveChanges();
                        result.Success();
                        result.Data = user.Password;
                    }
                    catch (Exception e)
                    {
                        Log.Error("ForgotPassword", e);
                    }
                }
            }
            return(result);
        }
Exemple #2
0
        public VarlikResult UpdatePassword(long idUser, UserUpdatePasswordDto userUpdatePasswordDto)
        {
            var result = new VarlikResult();

            using (var ctx = new VarlikContext())
            {
                var user = ctx.User.FirstOrDefault(l => !l.IsDeleted && l.Id == idUser && l.Password == userUpdatePasswordDto.OldPassword);
                if (user == null)
                {
                    result.Status = ResultStatus.NoSuchObject;
                    return(result);
                }
                user.Password = VarlikHasher.Hash(user.Password);
                user.Password = userUpdatePasswordDto.Password;
                try
                {
                    ctx.SaveChanges();
                    result.Success();
                }
                catch (Exception e)
                {
                    Log.Error("UpdatePassword", e);
                }
            }
            return(result);
        }
Exemple #3
0
        public VarlikResult <TokenUser> Login(string mail, string password)
        {
            var result = new VarlikResult <TokenUser>();

            using (var ctx = new VarlikContext())
            {
                password = VarlikHasher.Hash(password);

                var entity = ctx.User
                             .FirstOrDefault(l => l.Mail == mail.ToLower() &&
                                             l.Password == password &&
                                             !l.IsDeleted);

                if (entity == null)
                {
                    result.Status = ResultStatus.LoginFailed;
                    return(result);
                }

                result.Data = new TokenUser()
                {
                    Mail       = mail,
                    Id         = entity.Id,
                    CreatedAt  = DateTime.Now,
                    IsApproved = entity.IsUserApproved,
                    Name       = entity.Name,
                    Surname    = entity.Surname
                };
                result.Success();
            }
            return(result);
        }
Exemple #4
0
        public VarlikResult Register(UserDto userDto)
        {
            var result = new VarlikResult();

            using (var ctx = new VarlikContext())
            {
                result = userDto.IsValid();
                if (!result.IsSuccess)
                {
                    return(result);
                }

                var alreadyAdded = ctx.User.Any(l => l.Mail == userDto.Mail && !l.IsDeleted);
                if (alreadyAdded)
                {
                    result.Status = ResultStatus.AlreadyAdded;
                    return(result);
                }

                userDto.Password = VarlikHasher.Hash(userDto.Password);

                User user = new User()
                {
                    Mail               = userDto.Mail.ToLower(),
                    Password           = userDto.Password,
                    Name               = userDto.Name,
                    Surname            = userDto.Surname,
                    Phone              = userDto.Phone,
                    IsContractApproved = true
                };
                ctx.User.Add(user);
                try
                {
                    ctx.SaveChanges();
                    result.Success();
                }
                catch (Exception e)
                {
                    Log.Error("Register", e);
                }
            }
            return(result);
        }