public ValueTask <List <Invitation> > GetAllInvitations() { var context = Context; var invitationService = new InvitationService(context); return(invitationService.GetAllInvitations()); }
private async ValueTask <string> GenerateInvitationCode(SqlSugarClient context, long senderId, UserAuthority authority) { try { context.BeginTran(); var invitationService = new InvitationService(context); var invitation = new Invitation() { ObjectId = IdGenerator.CreateId(), Authority = authority, CreateTime = DateTime.UtcNow, InvitationCode = EncryptUtils.GenerateRandomString(32), ReceiverId = -1, SenderId = senderId, UsedTime = DateTime.MinValue }; var success = await invitationService.InsertAsync(invitation); context.CommitTran(); return(success ? invitation.InvitationCode : null); } catch (Exception e) { context.RollbackTran(); throw; } }
public async ValueTask <string> CreateRootUser() { var context = Context; var invitationService = new InvitationService(context); var userService = new UserService(context); if (await userService.CountAsync(it => true) == 0 && await invitationService.CountAsync(it => true) == 0) { var invitationCode = await GenerateInvitationCode(context, -1, UserAuthority.Root); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("\nRoot用户邀请码:"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(invitationCode); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(); } return(null); }
public async ValueTask <RegisterResult> Register(string name, string password, string invitationCode) { var context = Context; try { context.BeginTran(); var invitationService = new InvitationService(context); var invitation = await invitationService. GetSingleAsync(it => it.InvitationCode == invitationCode); if (invitation == null) { context.RollbackTran(); return(RegisterResult.InvitationCodeNotFound); } if (invitation.ReceiverId > 0) { context.RollbackTran(); return(RegisterResult.InvitationCodeHasBeenUsed); } var salt = EncryptUtils.GenerateRandomBytes(64); password = PasswordHashProvider.CreateHash(salt, password, 16); var user = new User { Id = IdGenerator.CreateId(), Name = name, Password = password, Salt = salt, Authority = invitation.Authority }; var userService = new UserService(context); var result = userService.Insert(user); if (result.ErrorList.Count != 0) { context.RollbackTran(); return(RegisterResult.UsernameRepeated); } var success = await invitationService.UpdateAsync( it => new Invitation() { UsedTime = DateTime.UtcNow, ReceiverId = user.Id }, it => it.ObjectId == invitation.ObjectId); if (!success) { context.RollbackTran(); return(RegisterResult.InvitationCodeHasBeenUsed); } context.CommitTran(); return(RegisterResult.Success); } catch { context.RollbackTran(); throw; } }