コード例 #1
0
        public ActionResult Add(AddApplicationPostModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectWithViewModel(Mapper.Map <AddApplicationPostModel, AddApplicationViewModel>(viewModel), "add"));
            }

            var response = _addApplicationCommand.Invoke(new AddApplicationRequest
            {
                Name                  = viewModel.Name,
                IsActive              = viewModel.Active,
                CurrentUser           = Core.AppContext.CurrentUser,
                MatchRuleFactoryId    = new MethodAndTypeMatchRuleFactory().Id,
                UserId                = viewModel.UserId,
                HipChatRoomId         = viewModel.HipChatRoomId,
                NotificationGroups    = viewModel.NotificationGroups.Where(n => n.Selected).Select(g => g.Id).ToList(),
                CampfireRoomId        = viewModel.CampfireRoomId,
                Version               = viewModel.Version,
                NotificationFrequency = viewModel.NotificationFrequency
            });

            if (response.Status != AddApplicationStatus.Ok)
            {
                return(RedirectWithViewModel(Mapper.Map <AddApplicationPostModel, AddApplicationViewModel>(viewModel), "add", response.Status.MapToResource(Resources.Application.ResourceManager)));
            }

            ConfirmationNotification("Application '{0}' added.".FormatWith(viewModel.Name));
            return(Redirect(Url.Applications()));
        }
コード例 #2
0
        public CreateOrganisationResponse Invoke(CreateOrganisationRequest request)
        {
            Trace("Starting...");

            var existingOrganisation = Session.MasterRaven
                                       .Query <Organisation, Indexing.Organisations>()
                                       .FirstOrDefault(o => o.Name == request.OrganisationName);

            if (existingOrganisation != null)
            {
                return(new CreateOrganisationResponse
                {
                    Status = CreateOrganisationStatus.OrganisationExists
                });
            }

            var freeTrialPlan = _getAvailablePaymentPlansQuery.Invoke(new GetAvailablePaymentPlansRequest()).Plans.FirstOrDefault(p => p.IsFreeTier);
            var timezone      = request.TimezoneId ?? "UTC";
            var date          = DateTime.UtcNow.ToDateTimeOffset(timezone);

            var organisation = new Organisation
            {
                Name          = request.OrganisationName,
                Status        = OrganisationStatus.Active,
                PaymentPlanId = freeTrialPlan?.Id,
                CreatedOnUtc  = DateTime.UtcNow,
                TimezoneId    = timezone,
                PaymentPlan   = freeTrialPlan,
                ApiKeySalt    = Membership.GeneratePassword(8, 1),
                Subscription  = new Subscription
                {
                    Status               = SubscriptionStatus.Trial,
                    StartDate            = date,
                    CurrentPeriodEndDate = date.AddMonths(1),
                    LastModified         = date
                },
                CallbackUrl = request.CallbackUrl,
            };

            var ravenInstance = _getRavenInstancesQuery.Invoke(new GetRavenInstancesRequest())
                                .RavenInstances
                                .FirstOrDefault(r => r.Active) ?? RavenInstance.Master();

            organisation.RavenInstance   = ravenInstance;
            organisation.RavenInstanceId = ravenInstance.Id;

            MasterStore(organisation);

            var existingUserOrgMap = Session.MasterRaven
                                     .Query <UserOrganisationMapping, UserOrganisationMappings>()
                                     .FirstOrDefault(u => u.EmailAddress == request.Email);

            if (existingUserOrgMap != null)
            {
                existingUserOrgMap.Organisations.Add(organisation.Id);
            }
            else
            {
                MasterStore(new UserOrganisationMapping
                {
                    EmailAddress  = request.Email,
                    Organisations = new List <string> {
                        organisation.Id
                    },
                    Password      = request.Password.Hash(),
                    PasswordToken = Guid.Empty,
                    Status        = UserStatus.Active,
                    SsoUser       = request.SpecialUser.IsIn(SpecialUser.AppHarbor),
                });
            }

            organisation.ApiKey = Convert.ToBase64String(
                Encoding.UTF8.GetBytes(
                    _encryptor.Encrypt("{0}|{1}".FormatWith(organisation.FriendlyId, organisation.ApiKeySalt))));

            Session.SetOrganisation(organisation);
            Session.BootstrapOrganisation(organisation);

            var group = new Group
            {
                Name           = request.OrganisationName,
                OrganisationId = organisation.Id
            };

            Store(group);

            var user = new User
            {
                Email     = request.Email,
                FirstName = request.FirstName,
                LastName  = request.LastName,
                Role      = UserRole.Administrator,
                GroupIds  = new List <string> {
                    group.Id
                },
                ActiveOrganisation = organisation,
                OrganisationId     = organisation.Id,
                SpecialUser        = request.SpecialUser,
            };

            Store(user);

            //update the organisation with the primary user
            organisation.PrimaryUserId = user.Id;

            var addApplicationResponse = _addApplicationCommand.Invoke(new AddApplicationRequest
            {
                CurrentUser        = user,
                IsActive           = true,
                MatchRuleFactoryId = new MethodAndTypeMatchRuleFactory().Id,
                Name = request.OrganisationName,
                NotificationGroups = new List <string> {
                    group.Id
                },
                UserId   = user.Id,
                IsSignUp = true,
            });

            //TODO: sync indexes
            Session.SynchroniseIndexes <Indexing.Organisations, Indexing.Users>();

            return(new CreateOrganisationResponse(request.Email)
            {
                OrganisationId = organisation.Id,
                UserId = user.Id,
                ApplicationId = addApplicationResponse.ApplicationId,
            });
        }