diff --git a/TIAM.Database/DataLayers/Users/UserDal.cs b/TIAM.Database/DataLayers/Users/UserDal.cs index 96f3043f..b6ec58eb 100644 --- a/TIAM.Database/DataLayers/Users/UserDal.cs +++ b/TIAM.Database/DataLayers/Users/UserDal.cs @@ -23,6 +23,11 @@ namespace TIAM.Database.DataLayers.Users public async Task CreateUserAsync(User user) { Context.Users.Add(user); + Profile profile = new Profile(); + profile.Id = Guid.NewGuid(); + var parts = user.EmailAddress.Split('@'); + profile.Name = parts[0]; + Context.Profiles.Add(profile); Console.WriteLine($@"Saving user to db {user.Id}, {user.EmailAddress}, {user.PhoneNumber}, {user.Password}"); return await Context.SaveChangesAsync() > 0; } diff --git a/TIAMMobileApp/wwwroot/index.html b/TIAMMobileApp/wwwroot/index.html index e701eb4d..f24b8d4e 100644 --- a/TIAMMobileApp/wwwroot/index.html +++ b/TIAMMobileApp/wwwroot/index.html @@ -22,9 +22,6 @@ - link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0/css/bootstrap.min.css"--> - - diff --git a/TIAMSharedUI/Pages/AppLaunchComponent.razor b/TIAMSharedUI/Pages/AppLaunchComponent.razor index 96693a5e..d42c0aac 100644 --- a/TIAMSharedUI/Pages/AppLaunchComponent.razor +++ b/TIAMSharedUI/Pages/AppLaunchComponent.razor @@ -151,9 +151,9 @@ foreach (UserProductMapping Permission in Permissions) { - var permissionToCheck = await ServiceProviderDataService.GetUserProductMappingByIdAsync(Permission.Id); - _logger.Debug($"calling IsPowerOf with values: {Permission.Id}, {Permission.Permissions}, {permissionToCheck.Permissions}, {3}"); - var driverPermissionResult = IsBitSet(permissionToCheck.Permissions, 3); + //var permissionToCheck = await ServiceProviderDataService.GetUserProductMappingByIdAsync(Permission.Id); + _logger.Debug($"calling IsPowerOf with values: {Permission.Id}, {Permission.Permissions}, {1}"); + var driverPermissionResult = IsBitSet(Permission.Permissions, 1); if (driverPermissionResult) { _isDriver = true; diff --git a/TIAMSharedUI/Pages/Login.razor.cs b/TIAMSharedUI/Pages/Login.razor.cs index c8ee6811..d419d631 100644 --- a/TIAMSharedUI/Pages/Login.razor.cs +++ b/TIAMSharedUI/Pages/Login.razor.cs @@ -12,6 +12,7 @@ using AyCode.Services.Loggers; using Microsoft.AspNetCore.Components.Authorization; using TIAM.Core.Consts; using TIAM.Entities.Users; +using TIAMWebApp.Shared.Application.Services; namespace TIAMSharedUI.Pages { @@ -157,12 +158,12 @@ namespace TIAMSharedUI.Pages /// This method stores the user data in the session service so we know during navigation that the user is logged in. /// /// - protected void SaveToSessionInfo(UserSessionModel user) + protected async Task SaveToSessionInfo(UserSessionModel user) { sessionService.User = user; sessionService.IsAuthenticated = true; sessionService.HasCompany = user.UserModelDto.UserProductMappings.Count > 0; - sessionService.IsDriver = CheckIfDriver(user.UserModelDto.UserProductMappings); + sessionService.IsDriver = await CheckIfDriver(user.UserModelDto.UserProductMappings); if (user.UserModelDto.Id == TiamConstClient.DevAdminIds[0] || user.UserModelDto.Id == TiamConstClient.DevAdminIds[1]) { sessionService.IsDevAdmin = true; @@ -177,12 +178,16 @@ namespace TIAMSharedUI.Pages BrowserConsoleLogWriter.Debug($"Saved to session: IsAuthenticated: {sessionService.IsAuthenticated}, HasCompany: {sessionService.HasCompany}, IsDriver: {sessionService.IsDriver}, IsDevAdmin: {sessionService.IsDevAdmin}, IsSysAdmin: {sessionService.IsSysAdmin}"); } - public bool CheckIfDriver(List Permissions) + public async Task CheckIfDriver(List Permissions) { bool _isDriver = false; + foreach (UserProductMapping Permission in Permissions) { - if (IsPowerOfTwoInSum(2, Permission.Permissions)) + //var permissionToCheck = await ServiceProviderDataService.GetUserProductMappingByIdAsync(Permission.Id); + BrowserConsoleLogWriter.Debug($"calling IsPowerOf with values: {Permission.Id}, {Permission.Permissions}, {1}"); + var driverPermissionResult = IsBitSet(Permission.Permissions, 1); + if (driverPermissionResult) { _isDriver = true; sessionService.DriverPersmissionId = Permission.Id; @@ -191,10 +196,16 @@ namespace TIAMSharedUI.Pages return _isDriver; } - public static bool IsPowerOfTwoInSum(int number, int power) + public bool IsBitSet(int number, int power) { + BrowserConsoleLogWriter.Debug($"called IsBitSet with values: {number}, {power}"); + int powerOfTwo = 1 << power; // Calculate 2^power - return (number & powerOfTwo) != 0; // Check if the bit at position `power` is set + + BrowserConsoleLogWriter.Debug($"powerOfTwo: {powerOfTwo}, {power}"); + + bool result = (number & powerOfTwo) != 0; // Check if the bit at position `power` is set + return result; } } } diff --git a/TIAMWebApp/Server/Controllers/UserAPIController.cs b/TIAMWebApp/Server/Controllers/UserAPIController.cs index efab911b..fad95e82 100644 --- a/TIAMWebApp/Server/Controllers/UserAPIController.cs +++ b/TIAMWebApp/Server/Controllers/UserAPIController.cs @@ -14,6 +14,7 @@ using TIAM.Entities.Profiles; using TIAM.Entities.Addresses; using TIAM.Services.Server.Logins; using ILogger = TIAM.Core.Loggers.ILogger; +using AyCode.Core.Helpers; namespace TIAMWebApp.Server.Controllers { @@ -197,7 +198,7 @@ namespace TIAMWebApp.Server.Controllers else { var user = JObject.Parse(serializedRegistrationModel.GetRawText()).ToObject(); - + bool result = false; if (user != null) { //add userModel to users array @@ -220,12 +221,22 @@ namespace TIAMWebApp.Server.Controllers _logger.Info($@"User to be created: {email}"); _logger.Info($@"User to be created: {phoneNumber}"); _logger.Info($@"User to be created: {password}"); - - await _userDal.CreateUserAsync(new User(userId, email, phoneNumber, password)); + //var hashedPassword = PasswordHasher.HashPassword(user.Password, PasswordHasher.GenerateDynamicSalt(userId)); + //await _userDal.CreateUserAsync(new User(userId, email, phoneNumber, hashedPassword)); + var createResult = await _loginService.RegistrationAsync(userId, email, password, phoneNumber); + if (createResult != AyCode.Core.Consts.AcErrorCode.Unset) + { + _logger.Error("Error:" + createResult.ToString()); + result = false; + } + else + { + result = true; + } } } - return Ok("yes"); + return Ok(result.ToString()); } } @@ -297,7 +308,8 @@ namespace TIAMWebApp.Server.Controllers var userId = Guid.NewGuid(); var email = user?.Email; var phoneNumber = user?.PhoneNumber; - var password = user?.Password; + var password = user?.Password; + var referralId = user?.ReferralId; if (email is null || phoneNumber is null || password is null) @@ -310,23 +322,32 @@ namespace TIAMWebApp.Server.Controllers _logger.Info($@"User to be created: {email}"); _logger.Info($@"User to be created: {phoneNumber}"); _logger.Info($@"User to be created: {password}"); - User userToCreate = new(userId, email, phoneNumber, password); - userToCreate.ProfileId = Guid.NewGuid(); + //User userToCreate = new(userId, email, phoneNumber, hashedPassword); + //userToCreate.ProfileId = Guid.NewGuid(); - userToCreate.Profile = new Profile(); - userToCreate.Profile.Id = userToCreate.ProfileId; - userToCreate.Profile.Name = "Guest - " + nameExtension; - userToCreate.RefferalId = referralId; - userToCreate.Profile.AddressId = Guid.NewGuid(); + //userToCreate.Profile = new Profile(); + //userToCreate.Profile.Id = userToCreate.ProfileId; + //userToCreate.Profile.Name = "Guest - " + nameExtension; + //userToCreate.RefferalId = referralId; + //userToCreate.Profile.AddressId = Guid.NewGuid(); - //Random rnd = new Random(); - userToCreate.Profile.Address = new Address(); - userToCreate.Profile.Address.Id = userToCreate.Profile.AddressId; - userToCreate.Profile.Address.AddressText = null; - userToCreate.Profile.Address.Latitude = null; //Math.Round(90 + rnd.NextDouble(), 8); - userToCreate.Profile.Address.Longitude = null; //Math.Round(180 + rnd.NextDouble(), 8); - result = await _userDal.AddUserAsync(userToCreate); - guestUser = await _userDal.GetUserModelDtoByIdAsync(userId, false); + ////Random rnd = new Random(); + //userToCreate.Profile.Address = new Address(); + //userToCreate.Profile.Address.Id = userToCreate.Profile.AddressId; + //userToCreate.Profile.Address.AddressText = null; + //userToCreate.Profile.Address.Latitude = null; //Math.Round(90 + rnd.NextDouble(), 8); + //userToCreate.Profile.Address.Longitude = null; //Math.Round(180 + rnd.NextDouble(), 8); + //result = await _userDal.AddUserAsync(userToCreate); + //guestUser = await _userDal.GetUserModelDtoByIdAsync(userId, false); + var createResult = await _loginService.RegistrationAsync(userId, email, password, phoneNumber); + if (createResult != AyCode.Core.Consts.AcErrorCode.Unset) + { + _logger.Error("Error:" + createResult.ToString()); + } + else + { + result = true; + } } }