internal static string CreateNewProject(string projectName, string userEmail, string clientId, int compilerVersion, ProjectType projectType = ProjectType.NEWPROJECT, ProjectStatus projectStatus = ProjectStatus.IDLE) { try { if (_kitsuneServer == null) { InitializeConnection(); } var kitsuneProjectCollection = _kitsuneDatabase.GetCollection <KitsuneProject>(KitsuneProjectsCollectionName); var projectId = ObjectId.GenerateNewId().ToString(); DateTime datetime = DateTime.Now; #region Create Kitsune Project BucketNames bucketNames = new BucketNames { source = AmazonAWSConstants.SourceBucketName, demo = AmazonAWSConstants.DemoBucketName, placeholder = AmazonAWSConstants.PlaceHolderBucketName, production = AmazonAWSConstants.ProductionBucketName }; KitsuneProject newProject = new KitsuneProject() { CreatedOn = datetime, UpdatedOn = datetime, IsArchived = false, ProjectId = projectId, ProjectName = projectName, UserEmail = userEmail, BucketNames = bucketNames, ProjectType = projectType, ProjectStatus = projectStatus, PublishedVersion = 0, Version = 1, CompilerVersion = compilerVersion }; kitsuneProjectCollection.InsertOne(newProject); #endregion #region Create Default Customer try { CreateDefaultWebsite(projectId, projectName, userEmail, clientId); } catch (Exception ex) { //Log Error creating New Customer } #endregion #region Create Settings File if (!CreateDefaultSettingsFile(projectId)) { //TODO Log error creating default settings file } #endregion #region Create Default Resources var projectResourceCreated = CreateDefaultStaticIndexResource(projectId); #endregion #region SendDeveloperEmail if (!string.IsNullOrEmpty(projectName)) { var UserCollection = _kitsuneDatabase.GetCollection <UserModel>(KitsuneUserCollectionName); var WebsiteCollection = _kitsuneDatabase.GetCollection <KitsuneWebsiteCollection>(KitsuneWebsiteCollectionName); var WebsiteUserCollection = _kitsuneDatabase.GetCollection <KitsuneWebsiteUserCollection>(KitsuneWebsiteUserCollectionName); var websiteDetail = WebsiteCollection.Find(x => x.ProjectId == projectId).FirstOrDefault(); if (websiteDetail != null) { string websiteId = websiteDetail._id; KitsuneWebsiteUserCollection websiteUser = WebsiteUserCollection.Find(x => x.WebsiteId.Equals(websiteId)).SortBy(x => x.CreatedOn).FirstOrDefault(); if (websiteUser != null) { string username = websiteUser.UserName; string password = websiteUser.Password; string developerId = websiteUser.DeveloperId; if (!String.IsNullOrEmpty(developerId)) { var userDetails = UserCollection.Find(x => x._id.Equals(developerId)).FirstOrDefault(); if (userDetails != null) { Dictionary <string, string> optionalParameters = new Dictionary <string, string> { { EnvConstants.Constants.EmailParam_KAdminUserName, websiteUser.UserName }, { EnvConstants.Constants.EmailParam_KAdminPassword, websiteUser.Password } }; optionalParameters[EnvConstants.Constants.EmailParam_KAdminUrl] = string.Format(EnvConstants.Constants.KAdminBaseUrl, websiteDetail.WebsiteUrl).ToLower(); optionalParameters[EnvConstants.Constants.EmailParam_ProjectName] = projectName; ///TODO : Put first name once its added on user creation optionalParameters[EnvConstants.Constants.EmailParam_DeveloperName] = userDetails.DisplayName; EmailHelper emailHelper = new EmailHelper(); emailHelper.SendGetKitsuneEmail(string.Empty, userDetails.UserName, MailType.DEFAULT_CUSTOMER_KADMIN_CREDENTIALS, null, optionalParameters); } } } } } #endregion return(projectId); } catch (Exception ex) { throw ex; } }
/// <summary> /// Kitsune website creation with default website user /// </summary> /// <param name="projectId"></param> /// <param name="websiteTag"></param> /// <param name="customerMail"></param> /// <param name="customerPhoneNumber"></param> /// <param name="developerEmail"></param> /// <param name="websiteUrl"></param> /// <param name="developerId"></param> /// <returns></returns> internal static KitsuneWebsiteCollection CreateNewWebsite(string projectId, string websiteTag, string customerMail, string customerPhoneNumber, string developerEmail, string websiteUrl, string developerId, string clientId, string customerName = null, string domain = null, string customWebsiteId = null, bool?isSSLEnabled = true) { try { if (_kitsuneServer == null) { InitializeConnection(); } var kitsuneWebsiteCollection = _kitsuneDatabase.GetCollection <KitsuneWebsiteCollection>(KitsuneWebsiteCollectionName); var websiteDNSCollection = _kitsuneDatabase.GetCollection <WebsiteDNSInfo>(KitsuneWebsiteDNSInfoCollectionName); var userCollection = _kitsuneDatabase.GetCollection <KitsuneWebsiteUserCollection>(KitsuneWebsiteUserCollectionName); DateTime datetime = DateTime.Now; ObjectId websiteId = ObjectId.GenerateNewId(); //if the custom website id is present and is valid objectid then assign it if (!string.IsNullOrEmpty(customWebsiteId)) { if (!ObjectId.TryParse(customWebsiteId, out websiteId)) { websiteId = ObjectId.GenerateNewId(); } } #region GENERATE USERNAME AND PASSWORD PasswordGenerator passwordGenerator = new PasswordGenerator(); var password = passwordGenerator.GeneratePassword(true, true, true, 7); var userName = passwordGenerator.GenerateUserName(websiteTag); if (String.IsNullOrEmpty(password)) { throw new Exception($"Error generating password"); } if (String.IsNullOrEmpty(userName)) { throw new Exception($"Error generating userName"); } #endregion #region VERIFY IF WEBSITETAG ALREADY PRESENT OR NOT var count = kitsuneWebsiteCollection.Count(x => x.WebsiteTag.Equals(websiteTag) && x.ClientId == clientId); if (count > 0) { throw new Exception("Unable to get the KitsuneTag"); ////String kitsuneTag = kitsuneUrl.Split('.').First(); //if (string.IsNullOrEmpty(websiteTag)) // throw new Exception("Unable to get the KitsuneTag"); //Random random = new Random(); //var basePlugin = BasePluginConfigGenerator.GetBasePlugin(clientId); //kitsuneUrl = kitsuneTag + random.Next(100, 999) + basePlugin.GetSubDomain(); } #endregion #region CREATE NEW WEBSITE IN WEBSITECOLLECTION (WITH SUBDOMAIN AS WEBSITEURL) KitsuneWebsiteCollection websiteObject = new KitsuneWebsiteCollection { _id = websiteId.ToString(), UpdatedOn = datetime, CreatedOn = datetime, DeveloperId = developerId, WebsiteTag = websiteTag, ProjectId = projectId, IsArchived = false, IsActive = false, WebsiteUrl = websiteUrl, ClientId = clientId }; kitsuneWebsiteCollection.InsertOne(websiteObject); #endregion #region CREATE NEW RECORD IN WEBSITEDNSCOLLECTION (FOR SUBDOMAIN AS ACTIVE STATUS AND ALSO FOR REQUESTED DOMAIN IF PROVIDED WITH PENDING STATUS) var websiteDNSObj = new WebsiteDNSInfo { CreatedOn = datetime, DNSStatus = DNSStatus.Active, DomainName = websiteUrl.ToUpper(), IsSSLEnabled = isSSLEnabled ?? true, RootPath = websiteUrl.ToUpper(), WebsiteId = websiteObject._id }; websiteDNSCollection.InsertOne(websiteDNSObj); if (!string.IsNullOrEmpty(domain)) { websiteDNSObj = new WebsiteDNSInfo { CreatedOn = datetime, DNSStatus = DNSStatus.Pending, DomainName = domain.ToUpper(), IsSSLEnabled = isSSLEnabled ?? false, //default is false for custom domain RootPath = domain.ToUpper(), WebsiteId = websiteObject._id }; websiteDNSCollection.InsertOne(websiteDNSObj); } #endregion #region CREATE NEW WEBISITE USER FOR THE ABOVE CREATED WEBSITE (WITH OWNER ACCESSTYPE) var websiteUser = new KitsuneWebsiteUserCollection { DeveloperId = developerId, WebsiteId = websiteObject._id, AccessType = KitsuneWebsiteAccessType.Owner, UserName = websiteTag, CreatedOn = DateTime.UtcNow, IsActive = true, Password = password, Contact = new ContactDetails { Email = customerMail, PhoneNumber = customerPhoneNumber, FullName = customerName ?? websiteTag }, }; userCollection.InsertOne(websiteUser); #endregion return(websiteObject); } catch (Exception ex) { throw ex; } }