/// <summary>
        /// Deletes a single image record from table storage
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="objectId"></param>
        /// <param name="imageGroupTypeNameKey"></param>
        /// <param name="imageGroupNameKey"></param>
        /// <param name="imageFormatNameKey"></param>
        /// <returns></returns>
        internal static DataAccessResponseType DeleteImageRecord(string accountId, string storagePartition, string imageFormatGroupTypeNameKey, string objectId, string imageGroupNameKey, string imageFormatNameKey)
        {
            var response = new DataAccessResponseType();

            var imageKey = imageGroupNameKey + "-" + imageFormatNameKey;

            //CloudTableClient cloudTableClient = Sahara.Core.Settings.Azure.Storage.StorageConnections.AccountsStorage.CreateCloudTableClient();
            CloudTableClient cloudTableClient = Settings.Azure.Storage.GetStoragePartitionAccount(storagePartition).CreateCloudTableClient();

            //Create and set retry policy
            //IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(1), 4);
            IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 4);

            cloudTableClient.DefaultRequestOptions.RetryPolicy = linearRetryPolicy;



            try
            {
                //Delete from MAIN table
                CloudTable cloudTable1 = cloudTableClient.GetTableReference(Sahara.Core.Common.Methods.SchemaNames.AccountIdToTableStorageName(accountId) + ImageRecordTableName(imageFormatGroupTypeNameKey));
                //cloudTable1.CreateIfNotExists();
                TableEntity productImageEntity1 = cloudTable1.CreateQuery <TableEntity>().Where(p => p.PartitionKey == objectId && p.RowKey == imageKey).FirstOrDefault();
                cloudTable1.Execute(TableOperation.Delete(productImageEntity1));


                //Delete from LISTINGS table (We allow this to fail silently in case this is not a listing image
                try
                {
                    CloudTable cloudTable2 = cloudTableClient.GetTableReference(Sahara.Core.Common.Methods.SchemaNames.AccountIdToTableStorageName(accountId) + ImageRecordListingTableName(imageFormatGroupTypeNameKey));
                    //cloudTable2.CreateIfNotExists();
                    TableEntity productImageEntity2 = cloudTable2.CreateQuery <TableEntity>().Where(p => p.PartitionKey == objectId && p.RowKey == imageKey).FirstOrDefault();
                    cloudTable2.Execute(TableOperation.Delete(productImageEntity2));
                }
                catch
                {
                }

                response.isSuccess = true;
            }
            catch (Exception e)
            {
                PlatformLogManager.LogActivity(
                    CategoryType.Error,
                    ActivityType.Error_Exception,
                    e.Message,
                    "Exception while attempting to delete image record/rowKey'" + imageKey + "' for " + imageFormatGroupTypeNameKey + " '" + objectId + "' in table storage",
                    accountId,
                    null,
                    null,
                    null,
                    null,
                    null,
                    System.Reflection.MethodBase.GetCurrentMethod().ToString(),
                    null
                    );
            }

            return(response);
        }
        public JsonNetResult UpdateTheme(string themeName)
        {
            var accountNameKey = AuthenticationCookieManager.GetAuthenticationCookie().AccountNameKey;

            var account = Common.GetAccountObject(accountNameKey);

            var response = new DataAccessResponseType();

            if (account.PaymentPlan.AllowThemes == false)
            {
                response = new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "Your account plan does not allow for additional or custom themes."
                };
            }
            else
            {
                var accountSettings = AccountSettings.GetAccountSettings_Internal(accountNameKey);

                //Make updates:
                accountSettings.Theme = themeName;

                response = AccountSettings.UpdateAccountSettings_Internal(accountNameKey, accountSettings);
            }


            JsonNetResult jsonNetResult = new JsonNetResult();

            jsonNetResult.Formatting = Formatting.Indented;
            jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime
            jsonNetResult.Data = response;

            return(jsonNetResult);
        }
        internal static DataAccessResponseType AssignPartition(string accountID, string sqlPartitionName)
        {
            DataAccessResponseType response = new DataAccessResponseType();

            try
            {
                if (Sql.Statements.UpdateStatements.UpdateSqlPartition(accountID, sqlPartitionName))
                {
                    response.isSuccess = true;

                    return(response);
                }
                else
                {
                    response.isSuccess    = false;
                    response.ErrorMessage = "An error occurred while updating the SqlPartition name, or the AccountID does not exist.";

                    return(response);
                }
            }
            catch (Exception e)
            {
                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to assign partition: " + sqlPartitionName + " to account: " + accountID,
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                response.isSuccess    = false;
                response.ErrorMessage = e.Message;

                return(response);
            }
        }
        public static DataAccessResponseType AuthenticateUser(string email, string password)
        {
            var response = new DataAccessResponseType();

            //Verifiy all prameters
            if (string.IsNullOrEmpty(email))
            {
                response.ErrorMessages.Add("Please include an email.");
            }
            if (string.IsNullOrEmpty(password))
            {
                response.ErrorMessages.Add("Please include a password.");
            }

            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
            {
                response.isSuccess    = false;
                response.ErrorMessage = "Not all parameters contain a value!";
                return(response);
            }


            try
            {
                //Get user with 'Login' info (username + password)
                response = PlatformUserManager.GetUserWithLogin(email, password);

                if (response.isSuccess)
                {
                    var user = (PlatformUserIdentity)response.ResponseObject; //<-- ResponseObject can be converted to PlatformUser by consuming application

                    //Validate that the user is active
                    if (!user.Active)
                    {
                        response.isSuccess = false;
                        response.ErrorMessages.Add("This user is not active.");
                    }


                    return(response);
                }
                else
                {
                    return(response);
                }
            }
            catch (Exception e)
            {
                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to authenticate platform user with email: " + email,
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = e.Message
                });
            }
        }
Beispiel #5
0
        internal static DataAccessResponseType StoreTag(TagTableEntity tagEntity, string storagePartition)
        {
            var response = new DataAccessResponseType();

            //CloudTableClient cloudTableClient = Settings.Azure.Storage.StorageConnections.AccountsStorage.CreateCloudTableClient();
            CloudTableClient cloudTableClient = Settings.Azure.Storage.GetStoragePartitionAccount(storagePartition).CreateCloudTableClient();

            //Create and set retry policy
            //IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(1), 4);
            IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 4);

            cloudTableClient.DefaultRequestOptions.RetryPolicy = linearRetryPolicy;


            TableOperation operation = TableOperation.Insert((tagEntity as TableEntity));

            try
            {
                var tableResult = tagEntity.cloudTable.Execute(operation);
                response.isSuccess = true; //tableResult.;
            }
            catch
            {
                response.isSuccess    = false; //tableResult.;
                response.ErrorMessage = "Tag exists";
            }


            return(response);
        }
Beispiel #6
0
        internal static DataAccessResponseType ResetSubsubsubcategoryOrdering(string sqlPartition, string schemaId, string subsubcategoryId)
        {
            var response = new DataAccessResponseType();

            StringBuilder sqlStatement = new StringBuilder();

            //SQL Statement =============================================================
            sqlStatement.Append("UPDATE  ");
            sqlStatement.Append(schemaId);
            sqlStatement.Append(".Subsubsubcategory ");
            sqlStatement.Append("SET OrderID = '0' ");
            sqlStatement.Append("WHERE SubsubcategoryID = '");
            sqlStatement.Append(subsubcategoryId);
            sqlStatement.Append("'; ");

            //SqlCommand sqlCommand = new SqlCommand(sqlStatement.ToString(), Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(sqlPartition));
            SqlCommand sqlCommand = Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(sqlPartition).CreateCommand();

            sqlCommand.CommandText = sqlStatement.ToString();

            sqlCommand.Connection.OpenWithRetry();
            int result = sqlCommand.ExecuteNonQueryWithRetry(); // returns Int indicating number of rows affected

            sqlCommand.Connection.Close();

            if (result > 0)
            {
                response.isSuccess = true;
            }

            return(response);
        }
Beispiel #7
0
        public static DataAccessResponseType UpdatePropertySymbolPlacement(Account account, PropertyModel property, string symbolPlacement)
        {
            var response = new DataAccessResponseType();

            if (symbolPlacement.ToLower() == "leading" || symbolPlacement.ToLower() == "trailing")
            {
                //Do nothing
            }
            else
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Symbol placement can only be 'leading' or 'trailing'"
                });
            }

            response.isSuccess = Sql.Statements.UpdateStatements.UpdatePropertySymbolPlacement(account.SqlPartition, account.SchemaName, property.PropertyID.ToString(), symbolPlacement.ToLower());

            if (response.isSuccess)
            {
                //Clear Property Caches:
                Caching.InvalidateAllPropertyCachesForAccount(account.AccountNameKey);
            }

            return(response);
        }
Beispiel #8
0
        public static DataAccessResponseType UpdatePropertyFacetableState(Account account, PropertyModel property, bool isFacetable)
        {
            if (property.PropertyTypeNameKey == "paragraph")
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot update the facetability of paragraph types"
                });
            }
            else if (property.PropertyTypeNameKey == "string")
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You cannot update the facetability of string types"
                });
            }

            var response = new DataAccessResponseType();

            response.isSuccess = Sql.Statements.UpdateStatements.UpdatePropertyFacetableState(account.SqlPartition, account.SchemaName, property.PropertyID.ToString(), isFacetable);

            if (response.isSuccess)
            {
                //Clear Property Caches:
                Caching.InvalidateAllPropertyCachesForAccount(account.AccountNameKey);
            }

            return(response);
        }
        internal static DataAccessResponseType AccountSubdomain(string accountNameKey, string content, string zoneId)
        {
            var response = new DataAccessResponseType();

            string type = "CNAME";

            string postContent = "{\"type\":\"" + type + "\",\"name\":\"" + accountNameKey + "\",\"content\":\"" + content + "\",\"ttl\":120,\"proxied\":true}";

            var httpContent = new StringContent(postContent, Encoding.UTF8, "application/json");

            httpContent.Headers.Add("X-Auth-Email", Settings.Services.CloudFlare.Account.email);
            httpContent.Headers.Add("X-Auth-Key", Settings.Services.CloudFlare.Account.apiKey);

            using (var httpClient = new HttpClient())
            {
                // Do the actual request and await the response
                var httpResponse = httpClient.PostAsync(Settings.Services.CloudFlare.Account.apiDomain + "/zones/" + zoneId + "/dns_records", httpContent).Result;

                // If the response contains content we want to read it!
                if (httpResponse.Content != null)
                {
                    //var responseContent = httpResponse.Content.ReadAsStringAsync();

                    string responseContent = httpResponse.Content.ReadAsStringAsync().Result;

                    DnsCreateResponse createResponse = JsonConvert.DeserializeObject <DnsCreateResponse>(responseContent);

                    response.isSuccess = createResponse.success;
                }
            }

            return(response);
        }
        internal static DataAccessResponseType DeleteSubdomain(string accountNameKey, string zoneId, string recordId)
        {
            var response = new DataAccessResponseType();

            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("X-Auth-Email", Settings.Services.CloudFlare.Account.email);
                httpClient.DefaultRequestHeaders.Add("X-Auth-Key", Settings.Services.CloudFlare.Account.apiKey);

                // Do the actual request and await the response
                var httpResponse = httpClient.DeleteAsync(Settings.Services.CloudFlare.Account.apiDomain + "/zones/" + zoneId + "/dns_records/" + recordId).Result;

                // If the response contains content we want to read it!
                if (httpResponse.Content != null)
                {
                    //var responseContent = httpResponse.Content.ReadAsStringAsync();

                    string responseContent = httpResponse.Content.ReadAsStringAsync().Result;

                    DnsDeleteResponse deleteResponse = JsonConvert.DeserializeObject <DnsDeleteResponse>(responseContent);

                    response.isSuccess = deleteResponse.success;
                }
            }

            return(response);
        }
        public static DataAccessResponseType ProcessFailedStripeChargeEvent(string stripeCustomerID, string stripeChargeId, string amount, string failureMessage, string stripeEventId)
        {
            /* FROM STRIPE DOCS (why we only focus on the charge failure since we ensure a card is always on file:
             *
             * This type of charge is a one off and does not require any dunning, just a simple email to all account owners regarding the charge issue
             *
             * */

            var response = new DataAccessResponseType();

            // 1. Get the account
            var account = AccountManager.GetAccount(stripeCustomerID);

            // 2. Get all owners for the account:
            var accountOwnerEmails = AccountManager.GetAccountOwnerEmails(stripeCustomerID);


            //email all account owners a copy of the associated invoice
            EmailManager.Send(
                accountOwnerEmails,
                Settings.Endpoints.Emails.FromBilling,
                Settings.Copy.EmailMessages.ChargeFailed.FromName,
                String.Format(Settings.Copy.EmailMessages.ChargeFailed.Subject),
                String.Format(Settings.Copy.EmailMessages.ChargeFailed.Body, account.AccountName, account.AccountNameKey, failureMessage),
                true);


            response.isSuccess = true;

            return(response);
        }
        public static DataAccessResponseType RemoveSubdomains(string accountNameKey)
        {
            var response = new DataAccessResponseType();

            //Get id's for all 3 subdomain records
            var accountAdminRecord   = Get.GetSubdomainDnsRecord(accountNameKey, Settings.Services.CloudFlare.Domain_AccountAdmin.Domain, Settings.Services.CloudFlare.Domain_AccountAdmin.ZoneId);
            var accountApiRecord     = Get.GetSubdomainDnsRecord(accountNameKey, Settings.Services.CloudFlare.Domain_AccountApi.Domain, Settings.Services.CloudFlare.Domain_AccountApi.ZoneId);
            var accountWebsiteRecord = Get.GetSubdomainDnsRecord(accountNameKey, Settings.Services.CloudFlare.Domain_AccountWebsite.Domain, Settings.Services.CloudFlare.Domain_AccountWebsite.ZoneId);

            if (accountAdminRecord == null && accountApiRecord == null && accountWebsiteRecord == null)
            {
                response.isSuccess    = false;
                response.ErrorMessage = "One or more subdomain entries for '" + accountNameKey + "' failed during deletion. Could not find DNS Records. Please update CloudFlare manually.";
            }


            //Remove records using record id's
            var accountAdminDelete   = Delete.DeleteSubdomain(accountNameKey, Settings.Services.CloudFlare.Domain_AccountAdmin.ZoneId, accountAdminRecord.id);
            var accountApiDelete     = Delete.DeleteSubdomain(accountNameKey, Settings.Services.CloudFlare.Domain_AccountApi.ZoneId, accountApiRecord.id);
            var accountWebsiteDelete = Delete.DeleteSubdomain(accountNameKey, Settings.Services.CloudFlare.Domain_AccountWebsite.ZoneId, accountWebsiteRecord.id);

            if (accountAdminDelete.isSuccess && accountApiDelete.isSuccess && accountWebsiteDelete.isSuccess)
            {
                response.isSuccess = true;
            }
            else
            {
                response.isSuccess    = false;
                response.ErrorMessage = "One or more subdomain entries for '" + accountNameKey + "' failed during deletion. Please update CloudFlare manually.";
            }

            return(response);
        }
        public static DataAccessResponseType ProcessStripeCustomerDelinquencyChangedEvent(string accountId, string storagePartition, bool newDelinquencyStatus)
        {
            var response = new DataAccessResponseType();

            if (newDelinquencyStatus == false)
            {
                //Account is no longer delinquent according to stripe.

                //Turn off Delinquent state
                AccountManager.UpdateAccountDelinquentStatus(accountId, false);

                //Clear DunningAttempts Table:
                PlatformBillingManager.ClearAutomaticDunningAttempt(accountId, storagePartition);
            }
            else if (newDelinquencyStatus == true)
            {
                //Account is delinquent according to stripe.

                //Turn on Delinquent state:
                AccountManager.UpdateAccountDelinquentStatus(accountId, true);
            }

            response.isSuccess = true;

            return(response);
        }
        //------------ Destroy Data ------

        internal static DataAccessResponseType DestroySqlSchemaAndTables(Account account)
        {
            var response = new DataAccessResponseType();

            try
            {
                return(Sql.Statements.StoredProcedures.DestroySchema(account.SchemaName, account.SqlPartition));
            }
            catch (Exception e)
            {
                response.isSuccess    = false;
                response.ErrorMessage = e.Message;

                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to destroy SQL schema and tables for : " + account.AccountName,
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                PlatformLogManager.LogActivity(CategoryType.Error, ActivityType.Error_Exception, "Error destroying schema for: '" + account.AccountName + "' on: ' " + account.SqlPartition + "'", "AccountID: '" + account.AccountID + "' AccountNameKey: '" + account.AccountNameKey + "' Error: '" + e.Message + "'", account.AccountID.ToString(), account.AccountName);
            }

            return(response);
        }
Beispiel #15
0
        public static DataAccessResponseType UpdatePropertyFacetInterval(Account account, PropertyModel property, int newFacetInterval)
        {
            var response = new DataAccessResponseType();

            if (property.PropertyTypeNameKey == "number")// || property.PropertyTypeNameKey == "money" || property.PropertyTypeNameKey == "double")
            {
                response.isSuccess = Sql.Statements.UpdateStatements.UpdatePropertyFacetInterval(account.SqlPartition, account.SchemaName, property.PropertyID.ToString(), newFacetInterval);
            }
            else
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Only numerical property types use facet ranges."
                });
            }

            if (response.isSuccess)
            {
                //Clear Property Caches:
                Caching.InvalidateAllPropertyCachesForAccount(account.AccountNameKey);
            }

            return(response);
        }
        internal static DataAccessResponseType StoreTheme(ThemeTableEntity themeTableEntity)
        {
            var response = new DataAccessResponseType();

            try
            {
                TableOperation operation = TableOperation.Insert((themeTableEntity as TableEntity));
                themeTableEntity.cloudTable.Execute(operation);

                response.isSuccess = true;

                return(response);
            }
            catch (Exception e)
            {
                //Log exception and email platform admins

                /*
                 * PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                 *  e,
                 *  "attempting to store notifications into table storage for: " + userId,
                 *  System.Reflection.MethodBase.GetCurrentMethod()
                 * );
                 */

                response.isSuccess    = true;
                response.ErrorMessage = e.Message;

                return(response);
            }
        }
        public static DataAccessResponseType DeleteImageFormat(Account account, string imageGroupTypeNameKey, string imageGroupNameKey, string imageFormatNameKey)
        {
            var response = new DataAccessResponseType();

            //Get image formats for this grouptype
            var imageFormats = GetImageFormats(account.AccountNameKey, imageGroupTypeNameKey);
            ImageFormatModel imageFormatToDelete = null;

            #region Extract Format from GroupType/Group

            foreach (ImageFormatGroupModel group in imageFormats)
            {
                if (group.ImageFormatGroupNameKey == imageGroupNameKey)
                {
                    foreach (ImageFormatModel format in group.ImageFormats)
                    {
                        if (format.ImageFormatNameKey == imageFormatNameKey)
                        {
                            imageFormatToDelete = format;
                        }
                    }
                }
            }

            #endregion

            #region Validate Process Allowed

            if (imageFormatToDelete == null)
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Image format does not exist!"
                });
            }

            //Make sure deletion is allowed
            if (!imageFormatToDelete.AllowDeletion)
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Deletion is not allowed on this image format."
                });
            }

            #endregion

            response.isSuccess = Sql.Statements.DeleteStatements.DeleteImageFormat(account.SqlPartition, account.SchemaName, imageFormatToDelete.ImageFormatID.ToString());

            //Clear Cache
            if (response.isSuccess)
            {
                Caching.InvalidateImageFormatCaches(account.AccountNameKey);
            }

            return(response);
        }
Beispiel #18
0
        public static DataAccessResponseType ClearStripeWebhooksLog()
        {
            var response = new DataAccessResponseType();

            try
            {
                int amountOfDays = Sahara.Core.Settings.Platform.GarbageCollection.StripeWebhookEventLogDaysToPurge;

                CloudTableClient cloudTableClient = Sahara.Core.Settings.Azure.Storage.StorageConnections.PlatformStorage.CreateCloudTableClient();

                //Create and set retry policy
                //IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(1), 4);
                IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 3);
                cloudTableClient.DefaultRequestOptions.RetryPolicy = linearRetryPolicy;

                CloudTable cloudTable = cloudTableClient.GetTableReference("stripewebhookeventslog");

                cloudTable.CreateIfNotExists();

                TableQuery <TableEntity> query = new TableQuery <TableEntity>()
                                                 .Where(TableQuery.GenerateFilterConditionForDate("DateTimeUTC", QueryComparisons.LessThanOrEqual, DateTimeOffset.UtcNow.AddDays(amountOfDays * -1)));

                var stripeWebhooks = cloudTable.ExecuteQuery(query);

                int count = stripeWebhooks.Count();

                foreach (var log in stripeWebhooks)
                {
                    cloudTable.Execute(TableOperation.Delete(log));
                }

                if (count > 0)
                {
                    //Log Garbage Collection
                    PlatformLogManager.LogActivity(
                        CategoryType.GarbageCollection,
                        ActivityType.GarbageCollection_StripeEventLog,
                        "Purged " + count.ToString("#,##0") + " item(s) from the stripe webhook events logs",
                        count.ToString("#,##0") + " stripe webhook event(s) past " + amountOfDays + " days have been purged"
                        );
                }

                response.isSuccess = true;
            }
            catch (Exception e)
            {
                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to clear stripe webhooks log",
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                response.isSuccess    = false;
                response.ErrorMessage = e.Message;
            }

            return(response);
        }
Beispiel #19
0
        public JsonNetResult RefundPayment(string accountId, string chargeId, decimal refundAmount)
        {
            DataAccessResponseType response = null;

            if (string.IsNullOrEmpty(accountId))
            {
                response = new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "Cannot apply refunds on closed/null accounts."
                };
            }
            else
            {
                var user = AuthenticationCookieManager.GetAuthenticationCookie();


                if (response == null)
                {
                    var accountBillingServiceClient = new AccountBillingService.AccountBillingServiceClient();

                    try
                    {
                        accountBillingServiceClient.Open();
                        response = accountBillingServiceClient.RefundPayment(
                            accountId,
                            chargeId,
                            refundAmount,
                            AuthenticationCookieManager.GetAuthenticationCookie().Id,
                            PlatformAdminSite.AccountBillingService.RequesterType.PlatformUser, Common.SharedClientKey
                            );//,
                              //user.Id,
                              //PlatformAdminSite.AccountManagementService.RequesterType.PlatformUser).ToList();

                        //Close the connection
                        WCFManager.CloseConnection(accountBillingServiceClient);
                    }
                    catch (Exception e)
                    {
                        #region Manage Exception

                        string exceptionMessage = e.Message.ToString();

                        var    currentMethod       = System.Reflection.MethodBase.GetCurrentMethod();
                        string currentMethodString = currentMethod.DeclaringType.FullName + "." + currentMethod.Name;

                        // Abort the connection & manage the exception
                        WCFManager.CloseConnection(accountBillingServiceClient, exceptionMessage, currentMethodString);

                        #endregion
                    }
                }
            }

            JsonNetResult jsonNetResult = new JsonNetResult();
            jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
            jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime
            jsonNetResult.Data = response;

            return(jsonNetResult);
        }
        internal static DataAccessResponseType DestroyDocumentCollection(Account account)
        {
            var response = new DataAccessResponseType {
                isSuccess = false
            };

            try
            {
                // Create new stopwatch & begin timing tasks
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                #region Connect to the DocumentDB Client & Get Collection Object

                //Get the DocumentDB Client
                //var client = Sahara.Core.Settings.Azure.DocumentDbClients.AccountDocumentClient;
                //var dbSelfLink = Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseSelfLink;
                //Sahara.Core.Settings.Azure.DocumentDbClients.AccountDocumentClient.OpenAsync();


                //Build a collection Uri out of the known IDs
                //(These helpers allow you to properly generate the following URI format for Document DB:
                //"dbs/{xxx}/colls/{xxx}/docs/{xxx}"
                Uri collectionUri = UriFactory.CreateDocumentCollectionUri(Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseId, account.DocumentPartition);

                #endregion

                #region Delete the document collection for this account

                var result = Sahara.Core.Settings.Azure.DocumentDbClients.AccountDocumentClient.DeleteDocumentCollectionAsync(collectionUri.ToString()).Result;

                #endregion
            }
            catch
            {
                #region Manual Task Instructions (if failure occurs)

                response.isSuccess = false;

                PlatformLogManager.LogActivity(
                    CategoryType.ManualTask,
                    ActivityType.ManualTask_DocumentDB,
                    "Deletion of Document Collection for closed account '" + account.AccountName + "' failed.",
                    "DocumentPartition: '" + account.DocumentPartition + "' partition for: '" + account.AccountName + "'. Please manually delete the '" + account.DocumentPartition + "' Document Collection",
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    System.Reflection.MethodBase.GetCurrentMethod().ToString()
                    );

                #endregion
            }

            return(response);
        }
Beispiel #21
0
        public static DataAccessResponseType DeletePropertyValue(Account account, PropertyModel property, PropertyValueModel propertyValue)
        {
            var response = new DataAccessResponseType();

            #region Check if property value is currently in use on ANY documents for this account

            #region Get any relevant documents (Legacy)

            /*
             * //Get the DocumentDB Client
             * var client = Sahara.Core.Settings.Azure.DocumentDB.DocumentClients.AccountDocumentClient;
             * //var dbSelfLink = Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseSelfLink;
             * client.OpenAsync();
             *
             * string sqlQuery = "SELECT Top 1 * FROM Products p WHERE p.Properties." + property.PropertyName + " = '" + propertyValue.PropertyValueName + "'";
             *
             * //Build a collection Uri out of the known IDs
             * //(These helpers allow you to properly generate the following URI format for Document DB:
             * //"dbs/{xxx}/colls/{xxx}/docs/{xxx}"
             * Uri collectionUri = UriFactory.CreateDocumentCollectionUri(Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseId, account.DocumentPartition);
             * //Uri storedProcUri = UriFactory.CreateStoredProcedureUri(Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseId, account.DocumentPartition, "UpdateProduct");
             *
             * var document = client.CreateDocumentQuery<Document>(collectionUri.ToString(), sqlQuery).AsEnumerable().FirstOrDefault();
             */
            #endregion

            #endregion

            #region Get any relevant documents (from Search)

            //$filter=tags/any(t: t eq '345')
            string searchFilter = property.SearchFieldName + "/any(s: s eq '" + propertyValue.PropertyValueName + "')";

            var documentSearchResult = ProductSearchManager.SearchProducts(account.SearchPartition, account.ProductSearchIndex, "", searchFilter, "relevance", 0, 1);

            #endregion

            if (documentSearchResult.Count > 0)
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Cannot delete a property value that is in use on a product. Please remove all associations before deleting."
                });
            }
            else
            {
                response.isSuccess = Sql.Statements.DeleteStatements.DeletePropertyValue(account.SqlPartition, account.SchemaName, propertyValue.PropertyValueID.ToString());
            }

            if (response.isSuccess)
            {
                //Clear Category Caches:
                Caching.InvalidateAllPropertyCachesForAccount(account.AccountNameKey);
            }

            return(response);
        }
        internal static DataAccessResponseType SetFeaturedProperties(string sqlPartition, string schemaId, Dictionary <string, int> featuredOrderingDictionary)
        {
            var response = new DataAccessResponseType();

            StringBuilder sqlStatement = new StringBuilder();


            //FIRST we set ALL to 0
            sqlStatement.Append("UPDATE ");
            sqlStatement.Append(schemaId);
            sqlStatement.Append(".Property ");
            sqlStatement.Append("SET FeaturedID = '0'; ");

            //THEN we apply order id to all

            //SQL Statement =============================================================
            foreach (KeyValuePair <string, int> ordering in featuredOrderingDictionary)
            {
                try
                {
                    sqlStatement.Append("UPDATE  ");
                    sqlStatement.Append(schemaId);
                    sqlStatement.Append(".Property ");

                    sqlStatement.Append("SET FeaturedID = '");
                    sqlStatement.Append(ordering.Value);
                    sqlStatement.Append("' ");

                    sqlStatement.Append("WHERE PropertyID = '");
                    sqlStatement.Append(ordering.Key);
                    sqlStatement.Append("'; ");
                }
                catch (Exception e)
                {
                    return(new DataAccessResponseType {
                        isSuccess = false, ErrorMessage = "An error occurred while looping through the dictionary: " + e.Message
                    });
                }
            }

            //SqlCommand sqlCommand = new SqlCommand(sqlStatement.ToString(), Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(sqlPartition));
            SqlCommand sqlCommand = Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(sqlPartition).CreateCommand();

            sqlCommand.CommandText = sqlStatement.ToString();

            sqlCommand.Connection.OpenWithRetry();
            int result = sqlCommand.ExecuteNonQueryWithRetry(); // returns Int indicating number of rows affected

            sqlCommand.Connection.Close();


            if (result > 0)
            {
                response.isSuccess = true;
            }

            return(response);
        }
        public static DataAccessResponseType ProcessStripeSubscriptionStatusChangedEvent(Account account, string newSubscriptionStatus, string previousSubscriptionStatus)
        {
            var response = new DataAccessResponseType();


            if (newSubscriptionStatus == "active" && previousSubscriptionStatus == "unpaid" ||
                newSubscriptionStatus == "active" && previousSubscriptionStatus == "past_due")
            {
                // Turn off delinquncy, clear dunning & reactive the account
                AccountManager.UpdateAccountActiveState(account.AccountID.ToString(), true);
                AccountManager.UpdateAccountDelinquentStatus(account.AccountID.ToString(), false);
                PlatformBillingManager.ClearAutomaticDunningAttempt(account.AccountID.ToString(), account.StoragePartition);
            }
            else if (newSubscriptionStatus == "past_due" && previousSubscriptionStatus == "active")
            {
                // Turn on delinquncy: The 'ProcessFailedStripeInvoicedChargeEvent' will handle the dunning emails
                AccountManager.UpdateAccountDelinquentStatus(account.AccountID.ToString(), true);
            }
            else if (newSubscriptionStatus == "unpaid" && previousSubscriptionStatus == "past_due")
            {
                // 1. Deactivate the account, assure that delinquent is still true send final email
                AccountManager.UpdateAccountActiveState(account.AccountID.ToString(), false);
                AccountManager.UpdateAccountDelinquentStatus(account.AccountID.ToString(), true);

                // 2. get the account
                //var account = AccountManager.GetAccount(accountId);

                // 3. Get all owners for the account:
                var accountOwnerEmails = AccountManager.GetAccountOwnerEmails(account.AccountID.ToString());

                //Stripe has marked the Subscritption as 'unpaid'. Messaing is an alert about account closure
                // 4. Email all account owners

                //email all account owners a copy of account closure email
                EmailManager.Send(
                    accountOwnerEmails,
                    Settings.Endpoints.Emails.FromAlerts,
                    Settings.Copy.EmailMessages.InvoicedChargeFailed_AccountDeactivated.FromName,
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_AccountDeactivated.Subject),
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_AccountDeactivated.Body, account.AccountName),
                    true);


                // 5. Email Platform Admins
                EmailManager.Send(
                    Settings.Endpoints.Emails.PlatformEmailAddresses,
                    Settings.Endpoints.Emails.FromPlatform,
                    "ACCOUNT DEACTIVATED",
                    "An account has been deactivated due to an unpaid subscription",
                    "AccountName: <b>" + account.AccountName + "</b><br/><br/>AccountID: <b>" + account.AccountID + "</b><br/>",
                    true);
            }

            response.isSuccess = true;

            return(response);
        }
        internal static DataAccessResponseType DeleteAccount(Account account)
        {
            var response = new DataAccessResponseType();

            var SqlDeleteStatements = new Sql.Statements.DeleteStatements(account.AccountID.ToString(), account.SqlPartition);

            SqlDeleteStatements.DeleteAccount();

            return(response);
        }
Beispiel #25
0
        internal static DataAccessResponseType DestroySchema(string schemaName, string databasePartitionName)
        {
            var response = new DataAccessResponseType();

            //SqlCommand sqlCommand = new SqlCommand("DestroySchema", Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(databasePartitionName));
            SqlCommand sqlCommand = Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(databasePartitionName).CreateCommand();

            sqlCommand.CommandText = "DestroySchema";


            try
            {
                sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                sqlCommand.Parameters.Add(new SqlParameter("@SchemaName", schemaName));
                sqlCommand.Parameters.Add(new SqlParameter("@WorkTest", 'w'));

                sqlCommand.Connection.OpenWithRetry();

                sqlCommand.ExecuteNonQueryWithRetry();

                sqlCommand.Connection.Close();

                response.isSuccess      = true;
                response.SuccessMessage = "Schema '" + schemaName + "', and all associated object have been destroyed on '" + databasePartitionName + "'";
            }
            catch (Exception e)
            {
                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to destroy for: " + schemaName,
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                PlatformLogManager.LogActivity(
                    CategoryType.ManualTask,
                    ActivityType.ManualTask_SQL,
                    "Stored Procedure 'DestroySchema' Failed on the '" + databasePartitionName + "' partition for schema '" + schemaName + "'",
                    sqlCommand.ToString(),
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    System.Reflection.MethodBase.GetCurrentMethod().ToString()
                    );

                response.isSuccess    = false;
                response.ErrorMessage = e.Message;
            }

            return(response);
        }
        public JsonNetResult UploadPhoto() //  HttpPostedFileBase file)     //  Object file)
        {
            var length = Request.ContentLength;
            var bytes  = new byte[length];

            Request.InputStream.Read(bytes, 0, length);

            var response = new DataAccessResponseType();
            var platformManagementServiceClient = new AccountManagementService.AccountManagementServiceClient();

            JsonNetResult jsonNetResult = new JsonNetResult();

            try
            {
                platformManagementServiceClient.Open();

                var user = AuthenticationCookieManager.GetAuthenticationCookie();

                response = platformManagementServiceClient.UpdateAccountUserProfilePhoto(
                    user.AccountID.ToString(),
                    user.Id,
                    bytes,
                    user.Id,
                    AccountAdminSite.AccountManagementService.RequesterType.Exempt, Common.SharedClientKey);

                //Close the connection
                WCFManager.CloseConnection(platformManagementServiceClient);
            }
            catch (Exception e)
            {
                #region Manage Exception

                string exceptionMessage = e.Message.ToString();

                var    currentMethod       = System.Reflection.MethodBase.GetCurrentMethod();
                string currentMethodString = currentMethod.DeclaringType.FullName + "." + currentMethod.Name;

                // Abort the connection & manage the exception
                WCFManager.CloseConnection(platformManagementServiceClient, exceptionMessage, currentMethodString);

                // Upate the response object
                response.isSuccess    = false;
                response.ErrorMessage = WCFManager.UserFriendlyExceptionMessage;
                //response.ErrorMessages[0] = exceptionMessage;

                #endregion
            }

            jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
            jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime
            jsonNetResult.Data = response;

            return(jsonNetResult);
        }
Beispiel #27
0
        public static DataAccessResponseType DeprovisionClosedAccounts()
        {
            var response = new DataAccessResponseType();

            try
            {
                // 1. Get list of AccountID's that are past their AccountEndDate and have ClosueApproved set to TRUE. These accounts are ready for deprovisioning:
                var accountsToDeprovision = Sql.Statements.SelectStatements.SelectClosedAccountsToDeprovision();
                if (accountsToDeprovision.Count > 0)
                {
                    foreach (string accountID in accountsToDeprovision)
                    {
                        // Get the Account
                        //var account = AccountManager.GetAccountByID(accountID, false);
                        var account = AccountManager.GetAccount(accountID, false, AccountManager.AccountIdentificationType.AccountID);

                        // Delete the account, associated user(s) and all data for each account ID the open up each data partition for future accounts:
                        var deprovisioningResponse = DeprovisioningManager.DeprovisionAccount(account);

                        // Log Custodian Activity
                        //PlatformLogManager.LogActivity(CategoryType.Custodian, ActivityType.Custodian_Scheduled_Task,
                        //account.AccountName + " has been deprovisioned. (" + account.AccountID + ") ",
                        //"Check deprovisioning log for details.", account.AccountID.ToString(), account.AccountName);
                    }

                    response.isSuccess      = true;
                    response.SuccessMessage = "Closed account(s) have been deprovisioned, see deprovisioning log for details.";
                }
                else
                {
                    // No accounts to deprovision...
                    response.isSuccess      = true;
                    response.SuccessMessage = "No accounts found for deprovisioning.";

                    // Log (Commented out to make custodian less noisy)
                    //PlatformLogManager.LogActivity(CustodianLogActivity.ScheduledTask, ""No accounts found for deprovisioning.");
                }
            }
            catch (Exception e)
            {
                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to deprovision closed accounts",
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                response.isSuccess    = false;
                response.ErrorMessage = e.Message;
            }

            return(response);
        }
Beispiel #28
0
        public static DataAccessResponseType ClearLogs()
        {
            var response = new DataAccessResponseType();

            //Clear logs
            //ToDo Seperate logs by month or year???

            response.isSuccess = true;
            //response.ErrorMessage = "Not implemented.";

            return(response);
        }
        public static DataAccessResponseType ProcessSuccessfulStripeChargeEvent(string stripeCustomerId, string stripeChargeId, string stripeCardId, string StripeInvoiceId, string StripeEventId)
        {
            var response = new DataAccessResponseType();

            //Get all owners for the account:
            var accountOwnerEmails = AccountManager.GetAccountOwnerEmails(stripeCustomerId);
            //Get the account
            //var account = AccountManager.GetAccountByStripeCustomerID(stripeCustomerID, false);
            var account = AccountManager.GetAccount(stripeCustomerId);

            //Get the associated Stripe Invoice:
            var stripeManager = new StripeManager();


            var stripeCharge = stripeManager.GetCharge(stripeChargeId);
            var stripeCard   = stripeManager.GetCard(stripeCustomerId, account.StripeCardID); //<-- in case card is updated we use the latest stripe card ID

            //Generate dollar amount
            var chargeTotalAmount = Sahara.Core.Common.Methods.Billing.ConvertStripeAmountToDollars(stripeCharge.Amount.ToString());

            //For HTML invoice or charge details string
            var htmlPaymentDetailsString = string.Empty;

            if (!String.IsNullOrEmpty(StripeInvoiceId))
            {
                //Charge has in invoice associated
                var stripeInvoice = stripeManager.GetInvoice(StripeInvoiceId);

                //Generate HTML invoice
                htmlPaymentDetailsString = Sahara.Core.Common.Methods.Billing.GenerateStripeHTMLInvoice(stripeInvoice);
            }
            else
            {
                //Generate HTML charge details (No associated invoice exists)
                htmlPaymentDetailsString = Sahara.Core.Common.Methods.Billing.GenerateStripeHTMLChargeDetails(stripeCharge);
            }

            //email all account owners a copy of the associated charge/invoice details
            EmailManager.Send(
                accountOwnerEmails,
                Settings.Endpoints.Emails.FromBilling,
                Settings.Copy.EmailMessages.AccountCharged.FromName,
                String.Format(Settings.Copy.EmailMessages.AccountCharged.Subject),
                String.Format(Settings.Copy.EmailMessages.AccountCharged.Body, account.AccountName, stripeCard.CardBrand, stripeCard.Last4, chargeTotalAmount, htmlPaymentDetailsString),
                true);

            //Clear Billing Issues Marker From Account
            //????/

            response.isSuccess = true;

            return(response);
        }
        internal static DataAccessResponseType AppendImageGalleryRecord(Account account, string imageGroupTypeNameKey, string objectId, string imageGroupName, string imageGroupNameKey, string imageFormatName, string imageFormatNameKey, string title, string description, string url, string filename, string filepath, string blobPath)
        {
            var response = new DataAccessResponseType();

            //CloudTableClient cloudTableClient = Settings.Azure.Storage.StorageConnections.AccountsStorage.CreateCloudTableClient();
            CloudTableClient cloudTableClient = Settings.Azure.Storage.GetStoragePartitionAccount(account.StoragePartition).CreateCloudTableClient();

            //Create and set retry policy--------
            IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 4);

            cloudTableClient.DefaultRequestOptions.RetryPolicy = linearRetryPolicy;

            CloudTable cloudTable = cloudTableClient.GetTableReference(Sahara.Core.Common.Methods.SchemaNames.AccountIdToTableStorageName(account.AccountID.ToString()) + Internal.ImageRecordTableStorage.ImageRecordTableName(imageGroupTypeNameKey));  //<-- accxxxxxproductimages / accxxxxxcategoryimages  / accxxxxxaccountimages

            //Get the entity to update
            var imageRecordEntity = (from record in cloudTable.CreateQuery <ImageRecordTableEntity>().Where(p => p.PartitionKey == objectId && p.ImageKey == imageGroupNameKey + "-" + imageFormatNameKey) select record).FirstOrDefault();


            //Validate maximum gallery images allowed:
            int currentCount = imageRecordEntity.Url.Split('|').Count();

            if (currentCount >= account.PaymentPlan.MaxImagesPerGallery)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You've reached your account limit of " + account.PaymentPlan.MaxImagesPerGallery + " images per gallery."
                });
            }


            //Append to image data
            imageRecordEntity.BlobPath    = imageRecordEntity.BlobPath + "|" + blobPath;
            imageRecordEntity.FileName    = imageRecordEntity.FileName + "|" + filename;
            imageRecordEntity.FilePath    = imageRecordEntity.FilePath + "|" + filepath;
            imageRecordEntity.Url         = imageRecordEntity.Url + "|" + url;
            imageRecordEntity.Title       = imageRecordEntity.Title + "|" + title;
            imageRecordEntity.Description = imageRecordEntity.Description + "|" + description;

            TableOperation operation = TableOperation.Replace((imageRecordEntity as TableEntity));

            try
            {
                //cloudTable.CreateIfNotExists();
                var tableResult = cloudTable.Execute(operation);
                response.isSuccess = true;
            }
            catch
            {
                response.isSuccess = false; //tableResult.;
            }


            return(response);
        }