/// <summary> /// Registers an item on a previous invite. /// </summary> /// <param name="registrationCode"></param> /// <param name="itemRegistrationCode"></param> /// <returns>Result of the registration operation.</returns> public ItemRegistrationResult RegisterItem(string registrationCode, out Lok.Unik.ModelCommon.ItemRegistration.ItemRegistration itemRegistrationCode) { var itemId = Guid.NewGuid(); var retval = new Lok.Unik.ModelCommon.ItemRegistration.ItemRegistrationResult { ItemId = itemId, PassCodeName = registrationCode, Result = ResultCode.RegistrationAccepted }; using (var coreSession = DocumentStoreLocator.Resolve(DocumentStoreLocator.RootLocation)) { // get the item matching the given registration code itemRegistrationCode = coreSession.Query <Lok.Unik.ModelCommon.ItemRegistration.ItemRegistration>().FirstOrDefault(dr => dr.PassCode == registrationCode); // not found? registration not accepted. if (null == itemRegistrationCode) { retval.ItemId = Guid.Empty; retval.AuthCode = string.Empty; retval.Result = ResultCode.RegistrationInvalidPasscode; return(retval); } // save the item authorization code used to log it in var ac = new Lok.Unik.ModelCommon.ItemRegistration.AuthCode { Principal = itemId.ToString(), AuthCodeId = Guid.NewGuid(), Code = AuthorizationCode.GenerateCode(), Tenant = itemRegistrationCode.TenancyId }; retval.AuthCode = ac.Code; retval.PassCodeId = itemRegistrationCode.Id; coreSession.Store(ac); coreSession.SaveChanges(); } return(retval); }
public virtual string PromoteAccount(string couponPassKey, BrokerTransactionType tt, string newRole, bool resetRole = true) { Debug.Assert(!string.IsNullOrEmpty(couponPassKey)); Debug.Assert(!string.IsNullOrEmpty(newRole)); string retval = null; BillingPlan bp = null; Coupon coupon = null; if (!string.IsNullOrEmpty(couponPassKey)) { if (GetBillingPlanFromCoupon(couponPassKey, out bp, out coupon)) { coupon.CurrentCount++; _dblog.InfoFormat("Coupon {0} now used {1} times of {2}", coupon.PassString, coupon.CurrentCount, coupon.TotalAllowed); } } if (null == bp) { bp = GetDefaultBillingPlan(); if (null == bp) { throw new AccountPromotionException("No default billing plan is available."); } } bool requiresPayment = bp.RecurrenceType > RecurrenceTypes._Paid; IUserContext uc = null; ApplicationUser user = null; if (requiresPayment) { string trxInfo = null; if (tt == BrokerTransactionType.Express) { _authCode = AuthorizationCode.GenerateCode(); trxInfo = AuthCode; _dblog.InfoFormat("Generated authcode {0} for express transaction button", trxInfo); } else if (tt == BrokerTransactionType.Upgrade) { uc = Catalog.Factory.Resolve <IUserContext>(); user = uc.GetCurrentUser(); trxInfo = user.PrincipalId; var subscr = (ApplicationUserSubscription)user.Extensions.GetOrAdd(ApplicationUserSubscription.Extension, _ => new ApplicationUserSubscription()); subscr.BillingPlan = bp; _dblog.InfoFormat("Generating upgrade payment button for user {0} to billing plan {1}", user.PrincipalId, bp.Name); } retval = BuildPaymentButton(trxInfo, bp, tt); } else { retval = null; uc = Catalog.Factory.Resolve <IUserContext>(); user = uc.GetCurrentUser(); var subscr = (ApplicationUserSubscription)user.Extensions.GetOrAdd(ApplicationUserSubscription.Extension, _ => new ApplicationUserSubscription()); Debug.Assert(null != user); if (null == user) { throw new AccountPromotionException("User not signed in."); } subscr.BillingPlan = bp; switch (bp.RecurrenceType) { case RecurrenceTypes.FreeForLife: if (resetRole) { user.AccountRoles.Clear(); } user.AccountRoles.Add(newRole); subscr.BillingPlan = bp; subscr.SubscriptionStart = DateTime.UtcNow; subscr.SubscriptionEnd = DateTime.MaxValue; subscr.BillingStatus = BillingStatus.NeverBilled; _log.InfoFormat("User {0} promoted to free for life", user.PrincipalId); break; case RecurrenceTypes.LimitedTrial: if (subscr.HadTrialAccount == couponPassKey) { _log.WarnFormat("User {0} already used trial account for {1}", user.PrincipalId, couponPassKey); throw new AccountPromotionException("Cannot use a trial account on this user."); } if (resetRole) { user.AccountRoles.Clear(); } user.AccountRoles.Add(newRole); subscr.SubscriptionStart = DateTime.UtcNow; subscr.SubscriptionEnd = DateTime.UtcNow + TimeSpan.FromDays(bp.GracePeriodDays); subscr.HadTrialAccount = couponPassKey; subscr.BillingStatus = BillingStatus.LimitedTrial; _log.InfoFormat("user {0} promoted to limited trial until {1} using coupon {2}", user.PrincipalId, subscr.SubscriptionEnd, couponPassKey); break; case RecurrenceTypes.Owner: user.AccountRoles.Add(newRole); subscr.BillingStatus = BillingStatus.NeverBilled; _log.WarnFormat("user {0} promoted to owner", user.PrincipalId); break; default: var bad = string.Format("Billing plan {0} has invalid recurrence type {1}", bp.Name, bp.RecurrenceType.ToString()); _log.ErrorFormat(bad); throw new AccessViolationException(bad); //break; } } using (var ds = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute)) { // user if (null != user) { ds.Store(user); } // coupon if (null != coupon) { ds.Store(coupon); } ds.SaveChanges(); } return(retval); }