private void AddInvitedUserToOrganization(string token, User user) { Invite invite; Organization organization = _organizationRepository.GetByInviteToken(token, out invite); if (organization != null) { if (!user.IsEmailAddressVerified && String.Equals(user.EmailAddress, invite.EmailAddress, StringComparison.OrdinalIgnoreCase)) { user.IsEmailAddressVerified = true; _userRepository.Update(user); } if (!_billingManager.CanAddUser(organization)) { ModelState.AddModelError(String.Empty, "Please upgrade your plan to add an additional user."); return; } user.OrganizationIds.Add(organization.Id); _userRepository.Update(user); organization.Invites.Remove(invite); _organizationRepository.Update(organization); _notificationSender.OrganizationUpdated(organization.Id); } }
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 if (String.IsNullOrEmpty(organization.Id)) { if (!_billingManager.CanAddOrganization(User.UserEntity)) { 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.UserEntity); organization = _organizationRepository.Add(organization); _notificationSender.OrganizationUpdated(organization.Id); User user = _userRepository.GetById(User.UserEntity.Id); user.OrganizationIds.Add(organization.Id); _userRepository.Update(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.UserEntity.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); _notificationSender.ProjectUpdated(organization.Id, project.Id); return(RedirectToAction("Configure", "Project", new { id = project.Id })); }