PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/sipsorcery-core/SIPSorcery.Web.Services/Provisioning/SIPProvisioningWebService.svc.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 826 lines | 662 code | 107 blank | 57 comment | 105 complexity | ae58b2cd537ef811f39fd74640333052 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------------
  2. // Filename: SIPProvisioningWebService.cs
  3. //
  4. // Description: Web services that expose provisioning services for SIP assets such
  5. // as SIPAccounts, SIPProivders etc. This web service deals with storing objects that need
  6. // to be presisted as oppossed to the manager web service which deals with transient objects
  7. // such as SIP acocunt bindings or registrations.
  8. //
  9. // History:
  10. // 25 Sep 2008 Aaron Clauson Created.
  11. //
  12. // License:
  13. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  14. //
  15. // Copyright (c) 2006 Aaron Clauson (aaronc@blueface.ie), Blue Face Ltd, Dublin, Ireland (www.blueface.ie)
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  19. // the following conditions are met:
  20. //
  21. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  22. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  23. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Blue Face Ltd.
  24. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  25. // prior written permission.
  26. //
  27. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  28. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  29. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  30. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  31. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. // POSSIBILITY OF SUCH DAMAGE.
  34. //-----------------------------------------------------------------------------
  35. using System;
  36. using System.Collections.Generic;
  37. using System.Linq.Dynamic;
  38. using System.Net;
  39. using System.ServiceModel;
  40. using System.ServiceModel.Activation;
  41. using System.ServiceModel.Description;
  42. using System.ServiceModel.Channels;
  43. using System.Text;
  44. using System.Data;
  45. using System.IO;
  46. using System.Linq;
  47. using System.Text.RegularExpressions;
  48. using System.Timers;
  49. using System.Threading;
  50. using System.Web;
  51. using System.Xml;
  52. using SIPSorcery.CRM;
  53. using SIPSorcery.Persistence;
  54. using SIPSorcery.SIP.App;
  55. using SIPSorcery.Sys;
  56. using log4net;
  57. namespace SIPSorcery.Web.Services
  58. {
  59. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  60. public class SIPProvisioningWebService : SIPSorceryAuthorisationService, IProvisioningService, IProvisioningServiceREST
  61. {
  62. private const string NEW_ACCOUNT_EMAIL_FROM_ADDRESS = "admin@sipsorcery.com";
  63. private const string NEW_ACCOUNT_EMAIL_SUBJECT = "SIP Sorcery Account Confirmation";
  64. private static string m_customerConfirmLink = AppState.GetConfigSetting("CustomerConfirmLink");
  65. private static string m_providerRegistrationsDisabled = AppState.GetConfigSetting("ProviderRegistrationsDisabled");
  66. private const string NEW_ACCOUNT_EMAIL_BODY =
  67. "Hi {0},\r\n\r\n" +
  68. "This is your automated SIP Sorcery new account confirmation email.\r\n\r\n" +
  69. "To confirm your account please visit the link below. If you did not request this email please ignore it.\r\n\r\n" +
  70. "{1}?id={2}\r\n\r\n" +
  71. "Regards,\r\n\r\n" +
  72. "SIP Sorcery";
  73. private ILog logger = AppState.GetLogger("provisioning");
  74. private SIPAssetPersistor<SIPAccount> SIPAccountPersistor;
  75. private SIPAssetPersistor<SIPDialPlan> DialPlanPersistor;
  76. private SIPAssetPersistor<SIPProvider> SIPProviderPersistor;
  77. private SIPAssetPersistor<SIPProviderBinding> SIPProviderBindingsPersistor;
  78. private SIPAssetPersistor<SIPRegistrarBinding> SIPRegistrarBindingsPersistor;
  79. private SIPAssetPersistor<SIPDialogueAsset> SIPDialoguePersistor;
  80. private SIPAssetPersistor<SIPCDRAsset> SIPCDRPersistor;
  81. private SIPDomainManager SIPDomainManager;
  82. private SIPMonitorLogDelegate LogDelegate_External = (e) => { };
  83. private int m_newCustomersAllowedLimit;
  84. private bool m_inviteCodeRequired;
  85. private bool m_providerRegDisabled;
  86. public SIPProvisioningWebService(
  87. SIPAssetPersistor<SIPAccount> sipAccountPersistor,
  88. SIPAssetPersistor<SIPDialPlan> sipDialPlanPersistor,
  89. SIPAssetPersistor<SIPProvider> sipProviderPersistor,
  90. SIPAssetPersistor<SIPProviderBinding> sipProviderBindingsPersistor,
  91. SIPAssetPersistor<SIPRegistrarBinding> sipRegistrarBindingsPersistor,
  92. SIPAssetPersistor<SIPDialogueAsset> sipDialoguePersistor,
  93. SIPAssetPersistor<SIPCDRAsset> sipCDRPersistor,
  94. CustomerSessionManager crmSessionManager,
  95. SIPDomainManager sipDomainManager,
  96. SIPMonitorLogDelegate log,
  97. int newCustomersAllowedLimit,
  98. bool inviteCodeRequired) :
  99. base(crmSessionManager)
  100. {
  101. SIPAccountPersistor = sipAccountPersistor;
  102. DialPlanPersistor = sipDialPlanPersistor;
  103. SIPProviderPersistor = sipProviderPersistor;
  104. SIPProviderBindingsPersistor = sipProviderBindingsPersistor;
  105. SIPRegistrarBindingsPersistor = sipRegistrarBindingsPersistor;
  106. SIPDialoguePersistor = sipDialoguePersistor;
  107. SIPCDRPersistor = sipCDRPersistor;
  108. SIPDomainManager = sipDomainManager;
  109. LogDelegate_External = log;
  110. m_newCustomersAllowedLimit = newCustomersAllowedLimit;
  111. m_inviteCodeRequired = inviteCodeRequired;
  112. if (!String.IsNullOrEmpty(m_providerRegistrationsDisabled))
  113. {
  114. Boolean.TryParse(m_providerRegistrationsDisabled, out m_providerRegDisabled);
  115. }
  116. }
  117. private string GetAuthorisedWhereExpression(Customer customer, string whereExpression)
  118. {
  119. try
  120. {
  121. if (customer == null)
  122. {
  123. throw new ArgumentNullException("customer", "The customer cannot be empty when building authorised where expression.");
  124. }
  125. if (customer.AdminId == Customer.TOPLEVEL_ADMIN_ID)
  126. {
  127. // This user is the top level administrator and has permission to view all system assets.
  128. return whereExpression;
  129. }
  130. else
  131. {
  132. string authorisedWhereExpression = "owner=\"" + customer.CustomerUsername + "\"";
  133. if (!customer.AdminId.IsNullOrBlank())
  134. {
  135. authorisedWhereExpression =
  136. "(owner=\"" + customer.CustomerUsername + "\" or adminmemberid=\"" + customer.AdminId + "\")";
  137. }
  138. if (!whereExpression.IsNullOrBlank())
  139. {
  140. authorisedWhereExpression += " and " + whereExpression;
  141. }
  142. return authorisedWhereExpression;
  143. }
  144. }
  145. catch (Exception excp)
  146. {
  147. logger.Error("Exception GetAuthorisedWhereExpression. " + excp.Message);
  148. throw new Exception("There was an exception constructing the authorisation filter for the request.");
  149. }
  150. }
  151. public bool IsAlive()
  152. {
  153. return base.IsServiceAlive();
  154. }
  155. public void TestException()
  156. {
  157. throw new ApplicationException("Test exception message " + DateTime.UtcNow.ToString("o") + ".");
  158. }
  159. public string Login(string username, string password)
  160. {
  161. return base.Authenticate(username, password);
  162. }
  163. public void ExtendSession(int minutes)
  164. {
  165. base.ExtendExistingSession(minutes);
  166. }
  167. public void Logout()
  168. {
  169. base.ExpireSession();
  170. }
  171. public bool AreNewAccountsEnabled()
  172. {
  173. logger.Debug("AreNewAccountsEnabled called from " + OperationContext.Current.Channel.RemoteAddress + ".");
  174. return m_newCustomersAllowedLimit == 0 || CRMCustomerPersistor.Count(c => !c.Suspended) < m_newCustomersAllowedLimit;
  175. }
  176. public void CreateCustomer(Customer customer)
  177. {
  178. try
  179. {
  180. if (m_inviteCodeRequired && customer.InviteCode == null)
  181. {
  182. throw new ApplicationException("Sorry new account creations currently require an invite code, please see http://sipsorcery.wordpress.com/new-accounts/.");
  183. }
  184. else if (m_newCustomersAllowedLimit != 0 && CRMCustomerPersistor.Count(null) >= m_newCustomersAllowedLimit)
  185. {
  186. // Check whether the number of customers is within the allowed limit.
  187. throw new ApplicationException("Sorry new account creations are currently disabled, please see http://sipsorcery.wordpress.com/new-accounts/.");
  188. }
  189. else
  190. {
  191. // Check whether the username is already taken.
  192. customer.CustomerUsername = customer.CustomerUsername.ToLower();
  193. Customer existingCustomer = CRMCustomerPersistor.Get(c => c.CustomerUsername == customer.CustomerUsername);
  194. if (existingCustomer != null)
  195. {
  196. throw new ApplicationException("The requested username is already in use please try a different one.");
  197. }
  198. // Check whether the email address is already taken.
  199. customer.EmailAddress = customer.EmailAddress.ToLower();
  200. existingCustomer = CRMCustomerPersistor.Get(c => c.EmailAddress == customer.EmailAddress);
  201. if (existingCustomer != null)
  202. {
  203. throw new ApplicationException("The email address is already associated with an account.");
  204. }
  205. string validationError = Customer.ValidateAndClean(customer);
  206. if (validationError != null)
  207. {
  208. throw new ApplicationException(validationError);
  209. }
  210. customer.MaxExecutionCount = Customer.DEFAULT_MAXIMUM_EXECUTION_COUNT;
  211. CRMCustomerPersistor.Add(customer);
  212. logger.Debug("New customer record added for " + customer.CustomerUsername + ".");
  213. // Create a default dialplan.
  214. SIPDialPlan defaultDialPlan = new SIPDialPlan(customer.CustomerUsername, "default", null, "sys.Log(\"hello world\")\n", SIPDialPlanScriptTypesEnum.Ruby);
  215. DialPlanPersistor.Add(defaultDialPlan);
  216. logger.Debug("Default dialplan added for " + customer.CustomerUsername + ".");
  217. // Get default domain name.
  218. string defaultDomain = SIPDomainManager.GetDomain("local", true);
  219. // Create SIP account.
  220. if (SIPAccountPersistor.Get(s => s.SIPUsername == customer.CustomerUsername && s.SIPDomain == defaultDomain) == null)
  221. {
  222. SIPAccount sipAccount = new SIPAccount(customer.CustomerUsername, defaultDomain, customer.CustomerUsername, customer.CustomerPassword, "default");
  223. SIPAccountPersistor.Add(sipAccount);
  224. logger.Debug("SIP account " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + " added for " + sipAccount.Owner + ".");
  225. }
  226. else
  227. {
  228. int attempts = 0;
  229. while (attempts < 10)
  230. {
  231. string testUsername = customer.CustomerUsername + Crypto.GetRandomString(4);
  232. if (SIPAccountPersistor.Get(s => s.SIPUsername == testUsername && s.SIPDomain == defaultDomain) == null)
  233. {
  234. SIPAccount sipAccount = new SIPAccount(customer.CustomerUsername, defaultDomain, testUsername, customer.CustomerPassword, "default");
  235. SIPAccountPersistor.Add(sipAccount);
  236. logger.Debug("SIP account " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + " added for " + sipAccount.Owner + ".");
  237. break;
  238. }
  239. else
  240. {
  241. attempts++;
  242. }
  243. }
  244. }
  245. if (!m_customerConfirmLink.IsNullOrBlank())
  246. {
  247. logger.Debug("Sending new account confirmation email to " + customer.EmailAddress + ".");
  248. 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));
  249. }
  250. else
  251. {
  252. logger.Debug("Customer confirmation email was not sent as no confirmation link has been set.");
  253. }
  254. }
  255. }
  256. catch (Exception excp)
  257. {
  258. logger.Error("Exception CreateNewCustomer. " + excp.Message);
  259. throw;
  260. }
  261. }
  262. public void DeleteCustomer(string customerUsername)
  263. {
  264. try
  265. {
  266. Customer customer = AuthoriseRequest();
  267. if (customer != null && customer.CustomerUsername == customerUsername)
  268. {
  269. CRMCustomerPersistor.Delete(customer);
  270. logger.Debug("Customer account " + customer.CustomerUsername + " successfully deleted.");
  271. }
  272. else
  273. {
  274. logger.Warn("Unauthorised attempt to delete customer " + customerUsername + ".");
  275. }
  276. }
  277. catch (Exception excp)
  278. {
  279. logger.Error("Exception DeleteCustomer. " + excp.Message);
  280. }
  281. }
  282. public Customer GetCustomer(string username)
  283. {
  284. Customer customer = AuthoriseRequest();
  285. if (customer.CustomerUsername == username)
  286. {
  287. return customer;
  288. }
  289. else
  290. {
  291. throw new ApplicationException("You are not authorised to retrieve customer for username " + username + ".");
  292. }
  293. }
  294. public int GetTimeZoneOffsetMinutes()
  295. {
  296. try
  297. {
  298. Customer customer = AuthoriseRequest();
  299. if (!customer.TimeZone.IsNullOrBlank())
  300. {
  301. foreach (TimeZoneInfo timezone in TimeZoneInfo.GetSystemTimeZones())
  302. {
  303. if (timezone.DisplayName == customer.TimeZone)
  304. {
  305. //return (int)timezone.BaseUtcOffset.TotalMinutes;
  306. return (int)timezone.GetUtcOffset(DateTimeOffset.UtcNow).TotalMinutes;
  307. }
  308. }
  309. }
  310. return 0;
  311. }
  312. catch (Exception excp)
  313. {
  314. logger.Error("Exception GetTimeZoneOffsetMinutes. " + excp.Message);
  315. return 0;
  316. }
  317. }
  318. public void UpdateCustomer(Customer updatedCustomer)
  319. {
  320. Customer customer = AuthoriseRequest();
  321. if (customer.CustomerUsername == updatedCustomer.CustomerUsername)
  322. {
  323. logger.Debug("Updating customer details for " + customer.CustomerUsername);
  324. customer.FirstName = updatedCustomer.FirstName;
  325. customer.LastName = updatedCustomer.LastName;
  326. customer.EmailAddress = updatedCustomer.EmailAddress;
  327. customer.SecurityQuestion = updatedCustomer.SecurityQuestion;
  328. customer.SecurityAnswer = updatedCustomer.SecurityAnswer;
  329. customer.City = updatedCustomer.City;
  330. customer.Country = updatedCustomer.Country;
  331. customer.WebSite = updatedCustomer.WebSite;
  332. customer.TimeZone = updatedCustomer.TimeZone;
  333. string validationError = Customer.ValidateAndClean(customer);
  334. if (validationError != null)
  335. {
  336. throw new ApplicationException(validationError);
  337. }
  338. CRMCustomerPersistor.Update(customer);
  339. }
  340. else
  341. {
  342. throw new ApplicationException("You are not authorised to update customer for username " + updatedCustomer.CustomerUsername + ".");
  343. }
  344. }
  345. public void UpdateCustomerPassword(string username, string oldPassword, string newPassword)
  346. {
  347. Customer customer = AuthoriseRequest();
  348. if (customer.CustomerUsername == username)
  349. {
  350. if (customer.CustomerPassword != oldPassword)
  351. {
  352. throw new ApplicationException("The existing password did not match when attempting a password update.");
  353. }
  354. else
  355. {
  356. logger.Debug("Updating customer password for " + customer.CustomerUsername);
  357. customer.CustomerPassword = newPassword;
  358. CRMCustomerPersistor.Update(customer);
  359. }
  360. }
  361. else
  362. {
  363. throw new ApplicationException("You are not authorised to update customer password for username " + username + ".");
  364. }
  365. }
  366. public List<SIPDomain> GetSIPDomains(string filterExpression, int offset, int count)
  367. {
  368. Customer customer = AuthoriseRequest();
  369. if (customer == null)
  370. {
  371. throw new ArgumentNullException("customer", "The customer cannot be empty when building authorised where expression.");
  372. }
  373. else
  374. {
  375. string authoriseExpression = "owner =\"" + customer.CustomerUsername + "\" or owner = null";
  376. //logger.Debug("SIPProvisioningWebService GetSIPDomains called for " + customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  377. return SIPDomainManager.Get(DynamicExpression.ParseLambda<SIPDomain, bool>(authoriseExpression), offset, count);
  378. }
  379. }
  380. public int GetSIPAccountsCount(string whereExpression)
  381. {
  382. Customer customer = AuthoriseRequest();
  383. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  384. //logger.Debug("SIPProvisioningWebService GetSIPAccountsCount called for " + customerSession.Customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  385. if (authoriseExpression.IsNullOrBlank())
  386. {
  387. return SIPAccountPersistor.Count(null);
  388. }
  389. else
  390. {
  391. return SIPAccountPersistor.Count(DynamicExpression.ParseLambda<SIPAccount, bool>(authoriseExpression));
  392. }
  393. }
  394. public List<SIPAccount> GetSIPAccounts(string whereExpression, int offset, int count)
  395. {
  396. Customer customer = AuthoriseRequest();
  397. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  398. logger.Debug("SIPProvisioningWebService GetSIPAccounts called for " + customer.CustomerUsername + " and where: " + authoriseExpression + ", offset=" + offset + ", count=" + count + ".");
  399. if (authoriseExpression.IsNullOrBlank())
  400. {
  401. return SIPAccountPersistor.Get(null, "sipusername", offset, count);
  402. }
  403. else
  404. {
  405. return SIPAccountPersistor.Get(DynamicExpression.ParseLambda<SIPAccount, bool>(authoriseExpression), "sipusername", offset, count);
  406. }
  407. }
  408. public SIPAccount AddSIPAccount(SIPAccount sipAccount)
  409. {
  410. Customer customer = AuthoriseRequest();
  411. sipAccount.Owner = customer.CustomerUsername;
  412. string validationError = SIPAccount.ValidateAndClean(sipAccount);
  413. if (validationError != null)
  414. {
  415. logger.Warn("Validation error in AddSIPAccount for customer " + customer.CustomerUsername + ". " + validationError);
  416. throw new ApplicationException(validationError);
  417. }
  418. else
  419. {
  420. return SIPAccountPersistor.Add(sipAccount);
  421. }
  422. }
  423. public SIPAccount UpdateSIPAccount(SIPAccount sipAccount)
  424. {
  425. Customer customer = AuthoriseRequest();
  426. if (customer.AdminId != Customer.TOPLEVEL_ADMIN_ID && sipAccount.Owner != customer.CustomerUsername)
  427. {
  428. logger.Debug("Unauthorised attempt to update SIP account by user=" + customer.CustomerUsername + ", on account owned by=" + sipAccount.Owner + ".");
  429. throw new ApplicationException("You are not authorised to update the SIP Account.");
  430. }
  431. string validationError = SIPAccount.ValidateAndClean(sipAccount);
  432. if (validationError != null)
  433. {
  434. logger.Warn("Validation error in UpdateSIPAccount for customer " + customer.CustomerUsername + ". " + validationError);
  435. throw new ApplicationException(validationError);
  436. }
  437. else
  438. {
  439. return SIPAccountPersistor.Update(sipAccount);
  440. }
  441. }
  442. public SIPAccount DeleteSIPAccount(SIPAccount sipAccount)
  443. {
  444. Customer customer = AuthoriseRequest();
  445. if (customer.AdminId != Customer.TOPLEVEL_ADMIN_ID && sipAccount.Owner != customer.CustomerUsername)
  446. {
  447. throw new ApplicationException("You are not authorised to delete the SIP Account.");
  448. }
  449. SIPAccountPersistor.Delete(sipAccount);
  450. // Enables the caller to see which SIP account has been deleted.
  451. return sipAccount;
  452. }
  453. public int GetSIPRegistrarBindingsCount(string whereExpression)
  454. {
  455. Customer customer = AuthoriseRequest();
  456. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  457. //logger.Debug("SIPProvisioningWebService GetSIPRegistrarBindingsCount for " + customerSession.Customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  458. if (authoriseExpression.IsNullOrBlank())
  459. {
  460. return SIPRegistrarBindingsPersistor.Count(null);
  461. }
  462. else
  463. {
  464. return SIPRegistrarBindingsPersistor.Count(DynamicExpression.ParseLambda<SIPRegistrarBinding, bool>(authoriseExpression));
  465. }
  466. }
  467. public List<SIPRegistrarBinding> GetSIPRegistrarBindings(string whereExpression, int offset, int count)
  468. {
  469. Customer customer = AuthoriseRequest();
  470. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  471. //logger.Debug("SIPProvisioningWebService GetSIPRegistrarBindings for " + customerSession.Customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  472. if (authoriseExpression.IsNullOrBlank())
  473. {
  474. return SIPRegistrarBindingsPersistor.Get(null, "sipaccountname", offset, count);
  475. }
  476. else
  477. {
  478. return SIPRegistrarBindingsPersistor.Get(DynamicExpression.ParseLambda<SIPRegistrarBinding, bool>(authoriseExpression), "sipaccountname", offset, count);
  479. }
  480. }
  481. public int GetSIPProvidersCount(string whereExpression)
  482. {
  483. Customer customer = AuthoriseRequest();
  484. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  485. //logger.Debug("SIPProvisioningWebService GetSIPProvidersCount for " + customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  486. if (authoriseExpression.IsNullOrBlank())
  487. {
  488. return SIPProviderPersistor.Count(null);
  489. }
  490. else
  491. {
  492. return SIPProviderPersistor.Count(DynamicExpression.ParseLambda<SIPProvider, bool>(authoriseExpression));
  493. }
  494. }
  495. public List<SIPProvider> GetSIPProviders(string whereExpression, int offset, int count)
  496. {
  497. Customer customer = AuthoriseRequest();
  498. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  499. if (authoriseExpression.IsNullOrBlank())
  500. {
  501. return SIPProviderPersistor.Get(null, "providername", offset, count);
  502. }
  503. else
  504. {
  505. //logger.Debug("SIPProvisioningWebService GetSIPProviders for " + customerSession.Customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  506. return SIPProviderPersistor.Get(DynamicExpression.ParseLambda<SIPProvider, bool>(authoriseExpression), "providername", offset, count);
  507. }
  508. }
  509. public SIPProvider AddSIPProvider(SIPProvider sipProvider)
  510. {
  511. Customer customer = AuthoriseRequest();
  512. sipProvider.Owner = customer.CustomerUsername;
  513. string validationError = SIPProvider.ValidateAndClean(sipProvider);
  514. if (validationError != null)
  515. {
  516. logger.Warn("Validation error in AddSIPProvider for customer " + customer.CustomerUsername + ". " + validationError);
  517. throw new ApplicationException(validationError);
  518. }
  519. else
  520. {
  521. return SIPProviderPersistor.Add(sipProvider);
  522. }
  523. }
  524. public SIPProvider UpdateSIPProvider(SIPProvider sipProvider)
  525. {
  526. Customer customer = AuthoriseRequest();
  527. if (customer.AdminId != Customer.TOPLEVEL_ADMIN_ID && sipProvider.Owner != customer.CustomerUsername)
  528. {
  529. throw new ApplicationException("You are not authorised to update the SIP Provider.");
  530. }
  531. string validationError = SIPProvider.ValidateAndClean(sipProvider);
  532. if (validationError != null)
  533. {
  534. logger.Warn("Validation error in UpdateSIPProvider for customer " + customer.CustomerUsername + ". " + validationError);
  535. throw new ApplicationException(validationError);
  536. }
  537. else
  538. {
  539. if (m_providerRegDisabled && sipProvider.RegisterEnabled)
  540. {
  541. logger.Warn("A SIP provider for customer " + customer.CustomerUsername + " had registrations enabled on a disabled registrations service.");
  542. throw new ApplicationException("SIP provider registrations are disabled on this system.");
  543. }
  544. else
  545. {
  546. return SIPProviderPersistor.Update(sipProvider);
  547. }
  548. }
  549. }
  550. public SIPProvider DeleteSIPProvider(SIPProvider sipProvider)
  551. {
  552. Customer customer = AuthoriseRequest();
  553. if (customer.AdminId != Customer.TOPLEVEL_ADMIN_ID && sipProvider.Owner != customer.CustomerUsername)
  554. {
  555. throw new ApplicationException("You are not authorised to delete the SIP Provider.");
  556. }
  557. //logger.Debug("DeleteSIPProvider, owner=" + sipProvider.Owner + ", providername=" + sipProvider.ProviderName + ".");
  558. SIPProviderPersistor.Delete(sipProvider);
  559. // Enables the caller to see which SIP Provider has been deleted.
  560. return sipProvider;
  561. }
  562. public int GetSIPProviderBindingsCount(string whereExpression)
  563. {
  564. Customer customer = AuthoriseRequest();
  565. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  566. if (authoriseExpression.IsNullOrBlank())
  567. {
  568. return SIPProviderBindingsPersistor.Count(null);
  569. }
  570. else
  571. {
  572. return SIPProviderBindingsPersistor.Count(DynamicExpression.ParseLambda<SIPProviderBinding, bool>(authoriseExpression));
  573. }
  574. }
  575. public List<SIPProviderBinding> GetSIPProviderBindings(string whereExpression, int offset, int count)
  576. {
  577. Customer customer = AuthoriseRequest();
  578. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  579. if (authoriseExpression.IsNullOrBlank())
  580. {
  581. return SIPProviderBindingsPersistor.Get(null, "providername asc", offset, count);
  582. }
  583. else
  584. {
  585. return SIPProviderBindingsPersistor.Get(DynamicExpression.ParseLambda<SIPProviderBinding, bool>(authoriseExpression), "providername asc", offset, count);
  586. }
  587. }
  588. public int GetDialPlansCount(string whereExpression)
  589. {
  590. Customer customer = AuthoriseRequest();
  591. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  592. //logger.Debug("SIPProvisioningWebService GetDialPlansCount for " + customerSession.Customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  593. if (authoriseExpression.IsNullOrBlank())
  594. {
  595. return DialPlanPersistor.Count(null);
  596. }
  597. else
  598. {
  599. return DialPlanPersistor.Count(DynamicExpression.ParseLambda<SIPDialPlan, bool>(authoriseExpression));
  600. }
  601. }
  602. public List<SIPDialPlan> GetDialPlans(string whereExpression, int offset, int count)
  603. {
  604. Customer customer = AuthoriseRequest();
  605. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  606. logger.Debug("SIPProvisioningWebService GetDialPlans for " + customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  607. if (authoriseExpression.IsNullOrBlank())
  608. {
  609. return DialPlanPersistor.Get(null, "dialplanname asc", offset, count);
  610. }
  611. else
  612. {
  613. return DialPlanPersistor.Get(DynamicExpression.ParseLambda<SIPDialPlan, bool>(authoriseExpression), "dialplanname asc", offset, count);
  614. }
  615. }
  616. public SIPDialPlan AddDialPlan(SIPDialPlan dialPlan)
  617. {
  618. Customer customer = AuthoriseRequest();
  619. dialPlan.Owner = customer.CustomerUsername;
  620. if (customer.AdminId != Customer.TOPLEVEL_ADMIN_ID)
  621. {
  622. dialPlan.MaxExecutionCount = SIPDialPlan.DEFAULT_MAXIMUM_EXECUTION_COUNT;
  623. }
  624. return DialPlanPersistor.Add(dialPlan);
  625. }
  626. public SIPDialPlan UpdateDialPlan(SIPDialPlan dialPlan)
  627. {
  628. Customer customer = AuthoriseRequest();
  629. if (customer.AdminId != Customer.TOPLEVEL_ADMIN_ID && dialPlan.Owner != customer.CustomerUsername)
  630. {
  631. throw new ApplicationException("You are not authorised to update the Dial Plan.");
  632. }
  633. return DialPlanPersistor.Update(dialPlan);
  634. }
  635. public SIPDialPlan DeleteDialPlan(SIPDialPlan dialPlan)
  636. {
  637. Customer customer = AuthoriseRequest();
  638. if (customer.AdminId != Customer.TOPLEVEL_ADMIN_ID && dialPlan.Owner != customer.CustomerUsername)
  639. {
  640. throw new ApplicationException("You are not authorised to delete the Dial Plan.");
  641. }
  642. DialPlanPersistor.Delete(dialPlan);
  643. // Enables the caller to see which dialplan has been deleted.
  644. return dialPlan;
  645. }
  646. public int GetCallsCount(string whereExpression)
  647. {
  648. try
  649. {
  650. Customer customer = AuthoriseRequest();
  651. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  652. //logger.Debug("SIPProvisioningWebService GetCallsCount for " + customerSession.Customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  653. if (authoriseExpression.IsNullOrBlank())
  654. {
  655. return SIPDialoguePersistor.Count(null);
  656. }
  657. else
  658. {
  659. return SIPDialoguePersistor.Count(DynamicExpression.ParseLambda<SIPDialogueAsset, bool>(authoriseExpression));
  660. }
  661. }
  662. catch (Exception excp)
  663. {
  664. logger.Error("Exception GetCallsCount. " + excp.Message);
  665. throw;
  666. }
  667. }
  668. public List<SIPDialogueAsset> GetCalls(string whereExpression, int offset, int count)
  669. {
  670. try
  671. {
  672. Customer customer = AuthoriseRequest();
  673. string authorisedExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  674. //logger.Debug("SIPProvisioningWebService GetCalls for " + customerSession.Customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  675. if (authorisedExpression.IsNullOrBlank())
  676. {
  677. return SIPDialoguePersistor.Get(null, null, offset, count);
  678. }
  679. else
  680. {
  681. return SIPDialoguePersistor.Get(DynamicExpression.ParseLambda<SIPDialogueAsset, bool>(authorisedExpression), null, offset, count);
  682. }
  683. }
  684. catch (Exception excp)
  685. {
  686. logger.Error("Exception GetCalls. " + excp.Message);
  687. throw;
  688. }
  689. }
  690. public int GetCDRsCount(string whereExpression)
  691. {
  692. Customer customer = AuthoriseRequest();
  693. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  694. //logger.Debug("SIPProvisioningWebService GetCDRsCount for " + customerSession.Customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  695. if (authoriseExpression.IsNullOrBlank())
  696. {
  697. return SIPCDRPersistor.Count(null);
  698. }
  699. else
  700. {
  701. return SIPCDRPersistor.Count(DynamicExpression.ParseLambda<SIPCDRAsset, bool>(authoriseExpression));
  702. }
  703. }
  704. public List<SIPCDRAsset> GetCDRs(string whereExpression, int offset, int count)
  705. {
  706. Customer customer = AuthoriseRequest();
  707. string authoriseExpression = GetAuthorisedWhereExpression(customer, whereExpression);
  708. //logger.Debug("SIPProvisioningWebService GetCDRs for " + customerSession.Customer.CustomerUsername + " and where: " + authoriseExpression + ".");
  709. if (authoriseExpression.IsNullOrBlank())
  710. {
  711. return SIPCDRPersistor.Get(null, "created desc", offset, count);
  712. }
  713. else
  714. {
  715. return SIPCDRPersistor.Get(DynamicExpression.ParseLambda<SIPCDRAsset, bool>(authoriseExpression), "created desc", offset, count);
  716. }
  717. }
  718. }
  719. }