Exemple #1
0
        public async Task<tbUser> CreateNewUser(dtoUser user)
        {
            tbUser newUser = new tbUser();

            try
            {
                newUser.UserId = Guid.NewGuid();
                newUser.Email = user.Email;
                newUser.IPAddress = user.IPAddress;
                newUser.UserRole = UserRole.User;
                newUser.Password = Crypter.Blowfish.Crypt(user.Password);
                newUser.CreatedDate = DateTime.UtcNow;
                newUser.IsDeleted = false;
                newUser.IsActive = true;

                _db.tbUsers.Add(newUser);
                await _db.SaveChangesAsync();
            }
            catch (DataException dex)
            {
                throw new ApplicationException("Data error!", dex);
            }

            return newUser;
        }
Exemple #2
0
        public async Task<tbUser> LoginUser(dtoUser user)
        {
            tbUser userObj = null;

            try
            {
                if (!string.IsNullOrEmpty(user.Email))
                    userObj = await GetUserByEmail(user.Email);
                else if (!string.IsNullOrEmpty(user.Username))
                    userObj = await GetUserByUserName(user.Username);

                if (userObj == null)
                    return userObj;

                if (!Crypter.CheckPassword(user.Password, userObj.Password))
                {
                    return new tbUser(); // password not match
                }
            }
            catch (DataException dex)
            {
                throw new ApplicationException("Data error!", dex);
            }

            return userObj;
        }
Exemple #3
0
        public async Task<IHttpActionResult> Login(dtoUser user)
        {
            dtoUserProfile userProfile = new dtoUserProfile();

            try
            {
                user.IPAddress = GetClientIp();
                userProfile = await _yuyanSvc.LoginUser(user);

                if (userProfile == null)
                    return Content(HttpStatusCode.NotFound, "User not found.");

                if (userProfile.UserId == Guid.Empty)
                    return Content(HttpStatusCode.Unauthorized, "Username and Password not match.");
            }
            catch (ApplicationException aex)
            {
                return BadRequest(aex.Message);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }

            return Ok(userProfile);
        }
Exemple #4
0
        public async Task TestRepo_FailedLogin_UnknownUser()
        {
            using (YuYanDBContext db = new YuYanDBContext())
            using (YuYanDBRepository repos = new YuYanDBRepository(db))
            {
                dtoUser testuser = new dtoUser() { Email = "*****@*****.**", Password = "******" };

                tbUser userobj = await repos.LoginUser(testuser);
                Assert.IsNull(userobj); // is null means the user not existed in the database
            }
        }
Exemple #5
0
 public async Task TestService_LoginUser_Failed_NoUser()
 {
     using (YuYanDBContext db = new YuYanDBContext())
     using (YuYanDBRepository repos = new YuYanDBRepository(db))
     {
         YuYanService svc = new YuYanService(repos);
         dtoUser newUser = new dtoUser() { Email = "*****@*****.**", Password = "******" };
         dtoUserProfile userObj = await svc.LoginUser(newUser);
         Assert.IsNull(userObj);
     }
 }
Exemple #6
0
 public async Task TestService_LoginUser_Success() {
     using (YuYanDBContext db = new YuYanDBContext())
     using (YuYanDBRepository repos = new YuYanDBRepository(db))
     {
         YuYanService svc = new YuYanService(repos);
         dtoUser newUser = new dtoUser() { Email = "*****@*****.**", Password = "******" };
         dtoUserProfile userObj = await svc.LoginUser(newUser);
         Assert.IsNotNull(userObj);
         Assert.AreEqual("*****@*****.**", userObj.Email);
     }
 }
Exemple #7
0
        public async Task TestRepo_SuccessLogin_User()
        {
            using (YuYanDBContext db = new YuYanDBContext())
            using (YuYanDBRepository repos = new YuYanDBRepository(db))
            {
                dtoUser testuser = new dtoUser() { Email = "*****@*****.**", Password = "******" };

                tbUser userobj = await repos.LoginUser(testuser);
                Assert.IsNotNull(userobj);
                Assert.AreEqual("*****@*****.**", userobj.Email, true);
            }
        }
Exemple #8
0
 public async Task TestService_LoginUser_Failed_WrongPassword()
 {
     using (YuYanDBContext db = new YuYanDBContext())
     using (YuYanDBRepository repos = new YuYanDBRepository(db))
     {
         YuYanService svc = new YuYanService(repos);
         dtoUser newUser = new dtoUser() { Email = "*****@*****.**", Password = "******" };
         dtoUserProfile userObj = await svc.LoginUser(newUser);
         Assert.IsNotNull(userObj);
         Assert.AreEqual(Guid.Empty, userObj.UserId);
     }
 }
Exemple #9
0
 public async Task<IHttpActionResult> CheckUser(dtoUser user)
 {
     dtoUser userObj = null;
     try
     {
         userObj = await _yuyanSvc.CheckUserAvailability(user.Email);
     }
     catch (ApplicationException aex)
     {
         return BadRequest(aex.Message);
     }
     catch (Exception ex)
     {
         return InternalServerError(ex);
     }
     return Ok(userObj);
 }
Exemple #10
0
        public static dtoUser ConvertToDtoUser(this tbUser source, dtoUser data = null)
        {
            if (data == null)
                data = new dtoUser();

            if (source == null)
                return null;

            data.UserId = source.UserId;
            data.Username = source.Username;
            data.Email = source.Email;
            data.IPAddress = source.IPAddress;
            data.IsActive = source.IsActive;
            data.IsDeleted = source.IsDeleted;

            return data;
        }
Exemple #11
0
        public async Task<IHttpActionResult> Register(dtoUser user)
        {
            dtoUserProfile userProfile = new dtoUserProfile();

            try
            {
                user.IPAddress = GetClientIp();
                userProfile = await _yuyanSvc.RegisterNewUser(user);
            }
            catch (ApplicationException aex)
            {
                return BadRequest(aex.Message);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }

            return Ok(userProfile);
        }
Exemple #12
0
        public async Task TestRepo_FailedLogin_WrongPasswordUser()
        {
            using (YuYanDBContext db = new YuYanDBContext())
            using (YuYanDBRepository repos = new YuYanDBRepository(db))
            {
                dtoUser testuser = new dtoUser() { Email = "*****@*****.**", Password = "******" };

                tbUser userobj = await repos.LoginUser(testuser);
                Assert.IsNotNull(userobj); // is new tbUser means the password not match
                Assert.AreEqual(Guid.Empty, userobj.UserId);
            }
        }