public UserAuthorizationM Login(UserLoginM model) { try { if (string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Password)) { throw BadRequest("Username and Password must not empty!"); } if (model.Username.Length < 3 || model.Password.Length < 3) { throw BadRequest("Username and Password must have more than 3 characters!"); } User user = _user.Where(u => u.Username.Equals(model.Username)) .Select(u => new User { Id = u.Id, Username = u.Username, Password = u.Password, AdminUserId = u.AdminUserId }).FirstOrDefault(); if (user == null) { throw BadRequest("Username or password is incorrect!"); } bool result = NococidAuthentication.VerifyHashedPassword(user.Username, user.Password, model.Password, out string rehashed_password); if (!result) { throw BadRequest("Username or password is incorrect!"); } if (rehashed_password != null) { user.Password = rehashed_password; } SaveChanges(); return(new UserAuthorizationM { User = new UserM { Id = user.Id, Username = user.Username }, HasVscAccount = _account.Any(a => a.UserId.Equals(user.Id)), AdminUser = user.AdminUserId == null ? null : _user.Where(u => u.Id.Equals(user.AdminUserId.Value)).Select(u => new UserM { Id = u.Id, Username = u.Username }).FirstOrDefault() }); } catch (Exception e) { throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while log in!", e, DateTime.Now, "Server", "Service_User_Login"); } }
public UserAuthorizationM Register(UserCreateM model, Guid?admin_user_id) { try { if (string.IsNullOrEmpty(model.Username) | string.IsNullOrEmpty(model.Password)) { throw BadRequest("The username or password not must emplty!"); } if (_user.Any(u => u.Username.Equals(model.Username))) { throw BadRequest("The username has been used!"); } User user = _user.Add(new User { Username = model.Username, Password = NococidAuthentication.GetHashedPassword(model.Username, model.Password), AdminUserId = admin_user_id }); SaveChanges(); UserAuthorizationM result = new UserAuthorizationM { HasVscAccount = false, User = new UserM { Id = user.Id, Username = user.Username } }; if (admin_user_id != null) { result.AdminUser = _user.Where(u => u.Id.Equals(admin_user_id)) .Select(u => new UserM { Id = u.Id, Username = u.Username }).FirstOrDefault(); } return(result); } catch (Exception e) { throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while register!", e, DateTime.Now, "Server", "Service_User_Register"); } }
public void Init(NococidContext context) { NoccidContext = context; try { int a = 0; short b = 1; short c = 2; byte[] bytes = new byte[] { 3, 4, 5, 6, 7, 8, 9, 10 }; VSCID.Github = new Guid(a++, b, c, bytes); RoleID.Admin = new Guid(a++, b, c, bytes); RoleID.Project_Manager = new Guid(a++, b, c, bytes); RoleID.Technical_Manager = new Guid(a++, b, c, bytes); RoleID.Project_Tester = new Guid(a++, b, c, bytes); RoleID.Developer = new Guid(a++, b, c, bytes); RoleID.All = new Guid[] { RoleID.Technical_Manager, RoleID.Project_Tester, RoleID.Developer }; ToolID.CircleCI = new Guid(a++, b, c, bytes); ToolID.Github = VSCID.Github; ToolID.Heroku = new Guid(a++, b, c, bytes); bool created = context.Database.EnsureCreatedAsync().GetAwaiter().GetResult(); context.SaveChangesAsync().GetAwaiter().GetResult(); if (created) { #region Role context.Role.Add(new Role { Id = RoleID.Project_Manager, Name = "Project Manager" }); context.Role.Add(new Role { Id = RoleID.Technical_Manager, Name = "Technical Manager" }); context.Role.Add(new Role { Id = RoleID.Project_Tester, Name = "Tester" }); context.Role.Add(new Role { Id = RoleID.Developer, Name = "Developer" }); context.Role.Add(new Role { Id = RoleID.Admin, Name = "Admin" }); #endregion #region Tool context.Tool.Add(new Tool { Id = ToolID.CircleCI, Name = "CircleCI", ToolType = "Build,Test,Deploy" }); context.Tool.Add(new Tool { Id = ToolID.Github, Name = "Github", ToolType = "Coding" }); context.Tool.Add(new Tool { Id = ToolID.Heroku, Name = "Heroku", ToolType = "Deploy" }); #endregion #region Project Type context.ProjectType.Add(new ProjectType { Name = "Desktop" }); context.ProjectType.Add(new ProjectType { Name = "Web Application" }); context.ProjectType.Add(new ProjectType { Name = "Mobile" }); #endregion #region User context.User.Add(new User { Username = ApplicationAuth.Nococid_Application_Admin, Password = NococidAuthentication.GetHashedPassword(ApplicationAuth.Nococid_Application_Admin, ApplicationAuth.Nococid_Application_Admin_Password) }); context.User.Add(new User { Username = "******", Password = NococidAuthentication.GetHashedPassword("ThangNLD", "zaq@123") }); context.User.Add(new User { Username = "******", Password = NococidAuthentication.GetHashedPassword("ToanLD", "zaq@123") }); #endregion context.SaveChangesAsync().GetAwaiter().GetResult(); } } catch (Exception e) { throw e; } }