public async Task <ResponseModel> ChangePasscode(int idUser, ChangePasscodeModel changePasscode) { ResponseModel result = new ResponseModel(); if (!UtilCommon.IsValidPassword(changePasscode.NewPasscode)) { result.Error = ErrorMessageCode.PASSWORD_INVALID; result.Status = System.Net.HttpStatusCode.BadRequest; return(result); } var user = await _userRepo.GetByIdAsync(idUser); if (user == null) { result.Error = ErrorMessageCode.USER_NOT_FOUND; result.Status = System.Net.HttpStatusCode.BadRequest; return(result); } if (!user.PassCode.Equals(UtilCommon.GeneratePasscode(changePasscode.OldPasscode, user.Salt))) { result.Error = ErrorMessageCode.PASSWORD_INVALID; result.Status = System.Net.HttpStatusCode.BadRequest; return(result); } try { string newPasscode = UtilCommon.GeneratePasscode(changePasscode.NewPasscode, user.Salt); user.PassCode = newPasscode; var res = await _userRepo.UpdateAsync(user); if (res != Constants.REPOSITORY_FAILED) { result.Status = System.Net.HttpStatusCode.OK; result.IsSuccess = true; if (user.IsFirstLogin) { user.IsFirstLogin = false; await _userRepo.UpdateAsync(user); } return(result); } else { result.Error = ErrorMessageCode.UPDATE_PASSWORD_FAILED; result.Status = System.Net.HttpStatusCode.InternalServerError; return(result); } } catch (Exception ex) { result.Error = ex.ToString(); result.Status = System.Net.HttpStatusCode.NotImplemented; return(result); } }
public async Task <ResponseModel> LoginUser(LoginModel loginModel) { ResponseModel result = new ResponseModel(); User user = null; if (!string.IsNullOrWhiteSpace(loginModel.Email)) { user = _userRepo.GetUserByEmailAsync(loginModel.Email, true); } if (user == null) { result.Error = ErrorMessageCode.USER_NOT_FOUND; result.Status = HttpStatusCode.NotFound; return(result); } if (user.IsDeactivate) { result.Error = ErrorMessageCode.USER_IS_DEACTIVATE; result.Status = HttpStatusCode.Forbidden; return(result); } try { var passcode = UtilCommon.GeneratePasscode(loginModel.PassCode, user.Salt); var expireToken = 1; if (passcode.Equals(user.PassCode)) { result.Status = System.Net.HttpStatusCode.OK; LoginApiModel data = new LoginApiModel(); data.IsFirstLogin = user.IsFirstLogin; data.Token = _jwtHandler.Create(user.Id.ToString(), expireToken, user.Role); data.Avatar = UtilCommon.GetDisplayImageUrl(user.Avatar); data.FullName = user.FullName; data.Role = user.Role; data.UserId = user.Id; result.Data = data; await _userRepo.UpdateAsync(user); return(result); } else { result.Error = ErrorMessageCode.PASSWORD_INCORRECT; result.Status = System.Net.HttpStatusCode.NotFound; var date = DateTime.UtcNow; return(result); } } catch (Exception ex) { result.Error = ex.ToString(); result.Status = System.Net.HttpStatusCode.NotImplemented; return(result); } }
public async Task <ResponseModel> CreateUser(UserApiModel model) { var response = new ResponseModel(); try { var user = model.ToEntity(new User()); user.Salt = Guid.NewGuid().ToString().Replace("-", ""); user.PassCode = UtilCommon.GeneratePasscode(model.PassWord, user.Salt); user.Role = UserRole.Student; var insert = await _userRepo.InsertAsync(user); if (insert > -1) { response.Data = insert; response.IsSuccess = true; response.Status = HttpStatusCode.OK; return(response); } else { response.IsSuccess = false; response.Error = ErrorMessageCode.SERVER_ERROR; response.Status = HttpStatusCode.InternalServerError; return(response); } } catch (Exception ex) { response.IsSuccess = false; response.Error = ex.ToString(); response.Status = HttpStatusCode.InternalServerError; return(response); } }
protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); #region HardCode var salt = new Guid().ToString(); var index = 1; modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "Mr Admin", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Administrator, Phone = "0132666666", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "Mr 1", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Teacher, Phone = "0132666665", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "Mr 2", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Teacher, Phone = "0132666664", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "Mr 3", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Teacher, Phone = "0132666663", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "Mr 4", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Teacher, Phone = "0132666662", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "Mr 5", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Teacher, Phone = "0132666661", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "Mr 6", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Teacher, Phone = "0132666660", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "St 1", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Student, Phone = "0132666667", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "St 2", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Student, Phone = "0132666668", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); modelBuilder.Entity <User>().HasData(new User { Id = index++, UserName = "******", Email = "*****@*****.**", FullName = "St 3", Salt = salt, PassCode = UtilCommon.GeneratePasscode("123456x@X", salt), Gender = Gender.Other, Role = UserRole.Student, Phone = "0132666669", IsDeactivate = false, IsFirstLogin = false, IsHardCode = true }); index = 1; modelBuilder.Entity <Course>().HasData(new Course { Id = index++, Name = "Logo Design Course", CourseCategory = CourseType.Design, Description = "Master of Design Architechture", OwnerId = 2, IsAvailable = true, IsDeactivate = false }); modelBuilder.Entity <Course>().HasData(new Course { Id = index++, Name = "Static Drawing", CourseCategory = CourseType.Drawing, Description = "Hand Drawing", OwnerId = 3, IsAvailable = true, IsDeactivate = false }); modelBuilder.Entity <Course>().HasData(new Course { Id = index++, Name = "English", CourseCategory = CourseType.Language, Description = "Master of English", OwnerId = 4, IsAvailable = true, IsDeactivate = false }); modelBuilder.Entity <Course>().HasData(new Course { Id = index++, Name = "Dominate The Dericurtive", CourseCategory = CourseType.Math, Description = "Calculate as A Calculater", OwnerId = 5, IsAvailable = true, IsDeactivate = false }); modelBuilder.Entity <Course>().HasData(new Course { Id = index++, Name = "JS in a nutshell", CourseCategory = CourseType.Programming, Description = "Become a full stack with JS", OwnerId = 6, IsAvailable = true, IsDeactivate = false }); modelBuilder.Entity <Course>().HasData(new Course { Id = index++, Name = "Criminal Act", CourseCategory = CourseType.Psycho, Description = "Learning how a crimer think", OwnerId = 7, IsAvailable = true, IsDeactivate = false }); index = 1; modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "UserRole", Created = DateTime.UtcNow, ParentId = null, Value = 0 }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Admin", Created = DateTime.UtcNow, ParentId = "UserRole", Value = (int)UserRole.Administrator }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Teacher", Created = DateTime.UtcNow, ParentId = "UserRole", Value = (int)UserRole.Teacher }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Student", Created = DateTime.UtcNow, ParentId = "UserRole", Value = (int)UserRole.Student }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Gender", Created = DateTime.UtcNow, ParentId = null, Value = 0 }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Male", Created = DateTime.UtcNow, ParentId = "Gender", Value = (int)Gender.Male }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Female", Created = DateTime.UtcNow, ParentId = "Gender", Value = (int)Gender.Female }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Other", Created = DateTime.UtcNow, ParentId = "Gender", Value = (int)Gender.Other }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "CourseType", Created = DateTime.UtcNow, ParentId = null, Value = 0 }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Design", Created = DateTime.UtcNow, ParentId = "CourseType", Value = (int)CourseType.Design }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Drawing", Created = DateTime.UtcNow, ParentId = "CourseType", Value = (int)CourseType.Drawing }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Language", Created = DateTime.UtcNow, ParentId = "CourseType", Value = (int)CourseType.Language }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Math", Created = DateTime.UtcNow, ParentId = "CourseType", Value = (int)CourseType.Math }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Programming", Created = DateTime.UtcNow, ParentId = "CourseType", Value = (int)CourseType.Programming }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Science", Created = DateTime.UtcNow, ParentId = "CourseType", Value = (int)CourseType.Science }); modelBuilder.Entity <HardCode>().HasData(new HardCode { Id = index++, Name = "Psycho", Created = DateTime.UtcNow, ParentId = "CourseType", Value = (int)CourseType.Psycho }); #endregion }