public void UpdateCustomerPassword(string username, string oldPassword, string newPassword)
        {
            Customer customer = AuthoriseRequest();

            if (customer.CustomerUsername == username)
            {
                if (PasswordHash.Hash(oldPassword, customer.Salt) != customer.CustomerPassword)
                {
                    throw new ApplicationException("The existing password did not match when attempting a password update.");
                }
                else
                {
                    logger.Debug("Updating customer password for " + customer.CustomerUsername);
                    //customer.CustomerPassword = newPassword;

                    // Hash the password.
                    string salt = PasswordHash.GenerateSalt();
                    customer.CustomerPassword = PasswordHash.Hash(newPassword, salt);
                    customer.Salt             = salt;

                    CRMCustomerPersistor.Update(customer);
                }
            }
            else
            {
                throw new ApplicationException("You are not authorised to update customer password for username " + username + ".");
            }
        }
        public void UpdateCustomer(Customer updatedCustomer)
        {
            Customer customer = AuthoriseRequest();

            if (customer.CustomerUsername == updatedCustomer.CustomerUsername)
            {
                logger.Debug("Updating customer details for " + customer.CustomerUsername);
                customer.FirstName        = updatedCustomer.FirstName;
                customer.LastName         = updatedCustomer.LastName;
                customer.EmailAddress     = updatedCustomer.EmailAddress;
                customer.SecurityQuestion = updatedCustomer.SecurityQuestion;
                customer.SecurityAnswer   = updatedCustomer.SecurityAnswer;
                customer.City             = updatedCustomer.City;
                customer.Country          = updatedCustomer.Country;
                customer.WebSite          = updatedCustomer.WebSite;
                customer.TimeZone         = updatedCustomer.TimeZone;

                string validationError = Customer.ValidateAndClean(customer);
                if (validationError != null)
                {
                    throw new ApplicationException(validationError);
                }

                CRMCustomerPersistor.Update(customer);
            }
            else
            {
                throw new ApplicationException("You are not authorised to update customer for username " + updatedCustomer.CustomerUsername + ".");
            }
        }
 public void DeleteCustomer(string customerUsername)
 {
     try
     {
         Customer customer = AuthoriseRequest();
         if (customer != null && customer.CustomerUsername == customerUsername)
         {
             CRMCustomerPersistor.Delete(customer);
             logger.Debug("Customer account " + customer.CustomerUsername + " successfully deleted.");
         }
         else
         {
             logger.Warn("Unauthorised attempt to delete customer " + customerUsername + ".");
         }
     }
     catch (Exception excp)
     {
         logger.Error("Exception DeleteCustomer. " + excp.Message);
     }
 }
        public void CreateCustomer(Customer customer)
        {
            try
            {
                if (m_inviteCodeRequired && customer.InviteCode == null)
                {
                    throw new ApplicationException("Sorry new account creations currently require an invite code, please see http://sipsorcery.wordpress.com/new-accounts/.");
                }
                else if (m_newCustomersAllowedLimit != 0 && CRMCustomerPersistor.Count(null) >= m_newCustomersAllowedLimit)
                {
                    // Check whether the number of customers is within the allowed limit.
                    throw new ApplicationException("Sorry new account creations are currently disabled, please see http://sipsorcery.wordpress.com/new-accounts/.");
                }
                else
                {
                    // Check whether the username is already taken.
                    customer.CustomerUsername = customer.CustomerUsername.ToLower();
                    Customer existingCustomer = CRMCustomerPersistor.Get(c => c.CustomerUsername == customer.CustomerUsername);
                    if (existingCustomer != null)
                    {
                        throw new ApplicationException("The requested username is already in use please try a different one.");
                    }

                    // Check whether the email address is already taken.
                    customer.EmailAddress = customer.EmailAddress.ToLower();
                    existingCustomer      = CRMCustomerPersistor.Get(c => c.EmailAddress == customer.EmailAddress);
                    if (existingCustomer != null)
                    {
                        throw new ApplicationException("The email address is already associated with an account.");
                    }

                    string validationError = Customer.ValidateAndClean(customer);
                    if (validationError != null)
                    {
                        throw new ApplicationException(validationError);
                    }

                    customer.MaxExecutionCount = Customer.DEFAULT_MAXIMUM_EXECUTION_COUNT;
                    customer.APIKey            = Crypto.GetRandomByteString(Customer.API_KEY_LENGTH / 2);

                    CRMCustomerPersistor.Add(customer);
                    logger.Debug("New customer record added for " + customer.CustomerUsername + ".");

                    // Create a default dialplan.
                    SIPDialPlan defaultDialPlan = new SIPDialPlan(customer.CustomerUsername, "default", null, "sys.Log(\"hello world\")\n", SIPDialPlanScriptTypesEnum.Ruby);
                    DialPlanPersistor.Add(defaultDialPlan);
                    logger.Debug("Default dialplan added for " + customer.CustomerUsername + ".");

                    // Get default domain name.
                    string defaultDomain = SIPDomainManager.GetDomain("local", true);

                    // Create SIP account.
                    if (SIPAccountPersistor.Get(s => s.SIPUsername == customer.CustomerUsername && s.SIPDomain == defaultDomain) == null)
                    {
                        SIPAccount sipAccount = new SIPAccount(customer.CustomerUsername, defaultDomain, customer.CustomerUsername, customer.CustomerPassword, "default");
                        SIPAccountPersistor.Add(sipAccount);
                        logger.Debug("SIP account " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + " added for " + sipAccount.Owner + ".");
                    }
                    else
                    {
                        int attempts = 0;
                        while (attempts < 10)
                        {
                            string testUsername = customer.CustomerUsername + Crypto.GetRandomString(4);
                            if (SIPAccountPersistor.Get(s => s.SIPUsername == testUsername && s.SIPDomain == defaultDomain) == null)
                            {
                                SIPAccount sipAccount = new SIPAccount(customer.CustomerUsername, defaultDomain, testUsername, customer.CustomerPassword, "default");
                                SIPAccountPersistor.Add(sipAccount);
                                logger.Debug("SIP account " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + " added for " + sipAccount.Owner + ".");
                                break;
                            }
                            else
                            {
                                attempts++;
                            }
                        }
                    }

                    if (!m_customerConfirmLink.IsNullOrBlank())
                    {
                        logger.Debug("Sending new account confirmation email to " + customer.EmailAddress + ".");
                        SIPSorcerySMTP.SendEmail(customer.EmailAddress, NEW_ACCOUNT_EMAIL_FROM_ADDRESS, NEW_ACCOUNT_EMAIL_SUBJECT, String.Format(NEW_ACCOUNT_EMAIL_BODY, customer.FirstName, m_customerConfirmLink, customer.Id));
                    }
                    else
                    {
                        logger.Debug("Customer confirmation email was not sent as no confirmation link has been set.");
                    }
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception CreateNewCustomer. " + excp.Message);
                throw;
            }
        }
 public bool AreNewAccountsEnabled()
 {
     logger.Debug("AreNewAccountsEnabled called from " + OperationContext.Current.Channel.RemoteAddress + ".");
     return(m_newCustomersAllowedLimit == 0 || CRMCustomerPersistor.Count(c => !c.Suspended) < m_newCustomersAllowedLimit);
 }