Exemplo n.º 1
0
        public async Task <IHttpActionResult> GetUserInfo()
        {
            ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

            var userId  = Guid.Parse(User.Identity.GetUserId());
            var user    = UnitOfWork.UsersRepository.Find(userId);
            var orgUser = user as OrgUser;

            var phoneNumber = await UserManager.GetPhoneNumberAsync(userId);

            var twoFactorAuth = await UserManager.GetTwoFactorEnabledAsync(userId);

            var userInfo = new UserInfoViewModel
            {
                UserId                         = CurrentUser.Id,
                Email                          = User.Identity.GetUserName(),
                EmailConfirmed                 = user.EmailConfirmed,
                PhoneNumber                    = phoneNumber,
                PhoneNumberConfirmed           = user.PhoneNumberConfirmed,
                HasRegistered                  = externalLogin == null,
                LoginProvider                  = externalLogin != null ? externalLogin.LoginProvider : null,
                Language                       = CurrentOrganisation?.DefaultLanguage?.Calture,
                Calendar                       = CurrentOrganisation?.DefaultCalendar?.SystemName,
                Roles                          = ServiceContext.UserManager.GetRoles(CurrentUser.Id),
                TwoFactorAuthenticationEnabled = twoFactorAuth,
            };

            if (orgUser != null)
            {
                userInfo.OrganisationId = CurrentOrganisationId;

                var subscriptionService = new SubscriptionService(UnitOfWork);
                var expiryDate          = subscriptionService.GetLatest(orgUser.Id);
                var lastSubscription    = subscriptionService.GetLastSubscription(orgUser.Id);
                var quota = subscriptionService.GetMonthlyQuota(orgUser.Id);

                userInfo.Profile = new UserProfileDTO
                {
                    FirstName        = orgUser.FirstName,
                    Surname          = orgUser.Surname,
                    Gender           = orgUser.Gender,
                    Birthdate        = orgUser.Birthdate,
                    Address          = orgUser.Address,
                    Email            = orgUser.Email,       // THIS COULD BE REMOVED AS WELL
                    PhoneNumber      = orgUser.PhoneNumber, // REMOVE THIS PROPERTY
                    IsSubscribed     = orgUser.IsSubscribed,
                    ExpiryDate       = expiryDate,
                    LastSubscription = lastSubscription,
                    MonthlyQuota     = quota
                };
            }

            return(Ok(userInfo));
        }
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var unitOfWork  = ServiceContext.UnitOfWork;
            var currentUser = ServiceContext.CurrentUser;

            if (currentUser is OrgUser && ((OrgUser)currentUser).AccountType == AccountType.MobileAccount)
            {
                var subscriptionService = new SubscriptionService(unitOfWork);
                var expiryDate          = subscriptionService.GetLatest(currentUser.Id);

                var fixedQuota       = GetFixedMonthlyQuota();
                var lastMonth        = DateTimeService.UtcNow.AddMonths(-1);
                var lastMonthRecords = unitOfWork.FilledFormsRepository.AllAsNoTracking
                                       .Count(x => x.FilledById == currentUser.Id && x.DateCreated >= lastMonth);

                if (expiryDate == null)
                {
                    // unsubscribed user. check fixed quota and if exceeded, return unauthorized.
                    if (lastMonthRecords >= fixedQuota)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized));
                    }
                }
                else
                {
                    var subscription = subscriptionService.GetLastSubscription(currentUser.Id);
                    if (subscription.Type == UserSubscriptionType.Paid && subscription.SubscriptionPlan.IsLimited)
                    {
                        var quota = subscription.SubscriptionPlan.MonthlyQuota.Value;
                        if (lastMonthRecords >= quota)
                        {
                            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized));
                        }
                    }

                    if (subscription.Type == UserSubscriptionType.Voucher)
                    {
                        // check fixed quota.
                        if (lastMonthRecords >= fixedQuota)
                        {
                            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized));
                        }
                    }
                }
            }

            base.OnActionExecuting(actionContext);
        }