public async Task <User> SignUp(string email, string username, string password) { var user = User.Create(email, username); if ((await userManager.CreateAsync(user, password)).Succeeded) { return(await rolesManager.AdmitRole(RoleName.User, user) ? user : throw new AuthException("Admitting user role failed")); } throw new AuthException("Creating account failed"); }
public async Task <PurchaseOrderResponse> Handle(PurchaseOrderRequest request, CancellationToken cancellationToken) { if (!await balanceValidationService.HasEnoughFunds(httpContextReader.CurrentUserId, request.TotalAmount)) { throw new PaymentException("Insufficient funds to complete transaction"); } var order = await orderService.PurchaseOrder(request); var orderOfferItems = order.Items.Where(i => i.Type == OrderType.Offer); var offersData = orderOfferItems.Select(i => new { OwnerId = i.OptionalData, Amount = (decimal)i.Amount / Constants.MoneyMultiplier }); foreach (var item in orderOfferItems) { var notification = await notifier.Push(NotificationMessages.OfferBoughtMessage(order.User.UserName, item.ProductName), item.OptionalData); await hubManager.Invoke(SignalrActions.NOTIFICATION_RECEIVED, item.OptionalData, mapper.Map <NotificationDto>(notification)); } var premiumOrder = order.Items.FirstOrDefault(i => i.Type == OrderType.Premium); if (premiumOrder != null && !await rolesManager.AdmitRole(RoleName.Premium, order.User)) { throw new PaymentException("Upgrading account to premium status failed"); } if (order != null) { foreach (var offerData in offersData) { await balanceService.AddBalance(offerData.OwnerId, offerData.Amount); await balanceService.AddBalance(order.UserId, -offerData.Amount); } string token = default(string); if (premiumOrder != null) { await balanceService.AddBalance(order.UserId, -(decimal)premiumOrder.Amount / Constants.MoneyMultiplier); token = await jwtAuthorizationTokenGenerator.GenerateToken(order.User); } return(new PurchaseOrderResponse { Order = mapper.Map <OrderDto>(order), Token = token }); } throw new PaymentException("Purchasing order failed"); }
protected async Task <IdentityResult> AddUserLogin(string provider, string providerKey, string email, string username = null, string pictureUrl = null) { var userLoginInfo = new UserLoginInfo(provider, providerKey, provider); var user = await userManager.FindByLoginAsync(userLoginInfo.LoginProvider, userLoginInfo.ProviderKey) ?? await userManager.FindByEmailAsync(email); if (user == null) { user = User.Create(email, username ?? email); await userManager.CreateAsync(user); } await userManager.AddLoginAsync(user, userLoginInfo); if (user == null) { throw new ExternalAuthException("Invalid external authentication"); } await rolesManager.AdmitRole(RoleName.ExternalUser, user); if (!user.IsRegistered()) { user.SetAvatarUrl(pictureUrl); await unitOfWork.Complete(); } if (UserBlockedSpecification.Create().IsSatisfied(user)) { throw new BlockException(); } var token = await jwtAuthorizationTokenGenerator.GenerateToken(user); return(new IdentityResult(token, user)); }