Esempio n. 1
0
        protected override PermissionResult CanAdd(Organization value)
        {
            if (String.IsNullOrEmpty(value.Name))
            {
                return(PermissionResult.DenyWithResult(BadRequest("Organization name is required.")));
            }

            if (!_billingManager.CanAddOrganization(ExceptionlessUser))
            {
                return(PermissionResult.DenyWithResult(PlanLimitReached("Please upgrade your plan to add an additional organization.")));
            }

            return(base.CanAdd(value));
        }
        protected override PermissionResult CanAdd(Organization value)
        {
            if (String.IsNullOrEmpty(value.Name))
            {
                return(PermissionResult.DenyWithMessage("Organization name is required."));
            }

            if (!IsOrganizationNameAvailableInternal(value.Name))
            {
                return(PermissionResult.DenyWithMessage("A organization with this name already exists."));
            }

            if (!_billingManager.CanAddOrganization(ExceptionlessUser))
            {
                return(PermissionResult.DenyWithPlanLimitReached("Please upgrade your plan to add an additional organization."));
            }

            return(base.CanAdd(value));
        }
Esempio n. 3
0
        protected override Organization InsertEntity(Organization value)
        {
            if (String.IsNullOrEmpty(value.Name))
            {
                throw new HttpResponseException(BadRequestErrorResponseMessage());
            }

            if (!IsNameAvailable(value.Name))
            {
                throw new HttpResponseException(DuplicateResponseMessage(value.Id));
            }

            if (!_billingManager.CanAddOrganization(User.UserEntity))
            {
                throw new HttpResponseException(PlanLimitReached("Please upgrade your plan to add an additional organization."));
            }

            _billingManager.ApplyBillingPlan(value, Settings.Current.EnableBilling ? BillingManager.FreePlan : BillingManager.UnlimitedPlan, User.UserEntity);

            value.ProjectCount    = 0;
            value.StackCount      = 0;
            value.ErrorCount      = 0;
            value.TotalErrorCount = 0;

            // TODO: User can currently specify the organization id to insert.
            Organization organization = base.InsertEntity(value);

            User user = _userRepository.GetById(User.UserEntity.Id);

            user.OrganizationIds.Add(organization.Id);
            _userRepository.Update(user);

            _notificationSender.OrganizationUpdated(organization.Id);

            return(organization);
        }
        public ActionResult Add(ProjectModel model)
        {
            ViewBag.Organizations    = Organizations;
            ViewBag.HasOrganizations = Organizations.Any();
            ViewBag.TimeZones        = TimeZoneInfo.GetSystemTimeZones();

            #region Validation

            Organization organization = null;

            if (!String.IsNullOrEmpty(model.OrganizationName))
            {
                Organization existing = Organizations.FirstOrDefault(o => o.Name == model.OrganizationName);
                organization = existing ?? new Organization {
                    Name = model.OrganizationName.Trim()
                };
            }
            else if (!String.IsNullOrEmpty(model.OrganizationId))
            {
                organization = Organizations.FirstOrDefault(o => o.Id == model.OrganizationId);
                ModelState state = ModelState["OrganizationName"];
                if (state != null)
                {
                    state.Errors.Clear();
                }
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (organization == null)
            {
                ModelState.AddModelError("OrganizationName", "Organization Name is required.");
                return(View(model));
            }

            if (!String.IsNullOrEmpty(organization.Id) && Projects.Count(p => p.OrganizationId == organization.Id && String.Equals(p.Name, model.Name, StringComparison.OrdinalIgnoreCase)) > 0)
            {
                ModelState.AddModelError("Name", "A project with this name already exists.");
                return(View(model));
            }

            #endregion

            var user = GetUser();
            if (String.IsNullOrEmpty(organization.Id))
            {
                if (!_billingManager.CanAddOrganization(user))
                {
                    ModelState.AddModelError(String.Empty, "Please upgrade your plan to add an additional organization.");
                    return(View(model));
                }

                _billingManager.ApplyBillingPlan(organization, Settings.Current.EnableBilling ? BillingManager.FreePlan : BillingManager.UnlimitedPlan, user);
                organization = _organizationRepository.Add(organization, true);

                user.OrganizationIds.Add(organization.Id);
                _userRepository.Save(user);
            }

            var project = new Project {
                Name = model.Name, TimeZone = model.TimeZone, OrganizationId = organization.Id
            };
            project.NextSummaryEndOfDayTicks = TimeZoneInfo.ConvertTime(DateTime.Today.AddDays(1), project.DefaultTimeZone()).ToUniversalTime().Ticks;
            project.ApiKeys.Add(Guid.NewGuid().ToString("N").ToLower());
            project.AddDefaultOwnerNotificationSettings(user.Id);

            if (!_billingManager.CanAddProject(project))
            {
                ModelState.AddModelError(String.Empty, "Please upgrade your plan to add an additional project.");
                return(View(model));
            }

            project = _projectRepository.Add(project);
            _organizationRepository.IncrementStats(project.OrganizationId, projectCount: 1);

            return(RedirectToAction("Configure", "Project", new { id = project.Id }));
        }