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

/sipsorcery-core/SIPSorcery.CRM/Customer.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 327 lines | 246 code | 46 blank | 35 comment | 76 complexity | 735ffa01fb50aaa32e3fe9d5a1733f8e MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // ============================================================================
  2. // FileName: Customer.cs
  3. //
  4. // Description:
  5. // Represents a customer record.
  6. //
  7. // Author(s):
  8. // Aaron Clauson
  9. //
  10. // History:
  11. // 20 May 2009 Aaron Clauson Created.
  12. //
  13. // License:
  14. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  15. //
  16. // Copyright (c) 2009 Aaron Clauson (aaronc@blueface.ie), Blue Face Ltd, Dublin, Ireland (www.blueface.ie)
  17. // All rights reserved.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  20. // the following conditions are met:
  21. //
  22. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  23. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  24. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Blue Face Ltd.
  25. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  26. // prior written permission.
  27. //
  28. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  29. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  30. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  31. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  32. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. // ============================================================================
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Linq;
  39. using System.Text;
  40. using System.Text.RegularExpressions;
  41. using System.Xml;
  42. using SIPSorcery.Sys;
  43. using SIPSorcery.Persistence;
  44. using log4net;
  45. #if !SILVERLIGHT
  46. using System.Data;
  47. using System.Data.Linq;
  48. using System.Data.Linq.Mapping;
  49. #endif
  50. namespace SIPSorcery.CRM
  51. {
  52. [Table(Name = "customers")]
  53. public class Customer : ISIPAsset
  54. {
  55. public const string XML_DOCUMENT_ELEMENT_NAME = "customers";
  56. public const string XML_ELEMENT_NAME = "customer";
  57. public const string TOPLEVEL_ADMIN_ID = "*"; // If a customer record has their AdminId set to this value they are in charge!
  58. public const int DEFAULT_MAXIMUM_EXECUTION_COUNT = 5; // The default value for the maximum allowed simultaneous executions of all the customer's dial plans.
  59. public const int MAX_FIELD_LENGTH = 64;
  60. public const int MIN_USERNAME_LENGTH = 5;
  61. public const int MAX_USERNAME_LENGTH = 20;
  62. public const int MIN_PASSWORD_LENGTH = 6;
  63. public const int MAX_PASSWORD_LENGTH = 20;
  64. public const int MAX_WEBSITE_FIELD_LENGTH = 256;
  65. public static readonly string USERNAME_ALLOWED_CHARS = @"a-zA-Z0-9_\-"; // The '.' character is not allowed in customer usernames in order to support a domain like structure for SIP account usernames.
  66. private static ILog logger = AppState.logger;
  67. private static string m_newLine = AppState.NewLine;
  68. [Column(Name = "id", DbType = "varchar(36)", IsPrimaryKey = true, CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  69. public Guid Id { get; set; }
  70. [Column(Name = "customerusername", DbType = "varchar(32)", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  71. public string CustomerUsername { get; set; }
  72. [Column(Name = "customerpassword", DbType = "varchar(32)", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  73. public string CustomerPassword { get; set; }
  74. [Column(Name = "emailaddress", DbType = "varchar(255)", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  75. public string EmailAddress { get; set; }
  76. [Column(Name = "lastname", DbType = "varchar(64)", UpdateCheck = UpdateCheck.Never)]
  77. public string LastName { get; set; }
  78. [Column(Name = "firstname", DbType = "varchar(64)", UpdateCheck = UpdateCheck.Never)]
  79. public string FirstName { get; set; }
  80. [Column(Name = "city", DbType = "varchar(64)", UpdateCheck = UpdateCheck.Never)]
  81. public string City { get; set; }
  82. [Column(Name = "country", DbType = "varchar(64)", UpdateCheck = UpdateCheck.Never)]
  83. public string Country { get; set; }
  84. [Column(Name = "website", DbType = "varchar(256)", UpdateCheck = UpdateCheck.Never)]
  85. public string WebSite { get; set; }
  86. [Column(Name = "active", DbType = "bit", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  87. public bool Active{get; set;}
  88. [Column(Name = "suspended", DbType = "bit", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  89. public bool Suspended{get; set;}
  90. [Column(Name = "suspendedreason", DbType = "varchar(1024)", UpdateCheck = UpdateCheck.Never)]
  91. public string SuspendedReason { get; set; }
  92. [Column(Name = "securityquestion", DbType = "varchar(1024)", UpdateCheck = UpdateCheck.Never)]
  93. public string SecurityQuestion { get; set; }
  94. [Column(Name = "securityanswer", DbType = "varchar(256)", UpdateCheck = UpdateCheck.Never)]
  95. public string SecurityAnswer { get; set; }
  96. [Column(Name = "createdfromipaddress", DbType = "varchar(15)", UpdateCheck = UpdateCheck.Never)]
  97. public string CreatedFromIPAddress { get; set; }
  98. [Column(Name = "adminid", DbType = "varchar(32)", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
  99. public string AdminId { get; set; } // Like a whitelabelid. If set identifies this user as the administrative owner of all accounts that have the same value for their adminmemberid.
  100. [Column(Name = "adminmemberid", DbType = "varchar(32)", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
  101. public string AdminMemberId { get; set; } // If set it designates this customer as a belonging to the administrative domain of the customer with the same adminid.
  102. [Column(Name = "timezone", DbType = "varchar(128)", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
  103. public string TimeZone { get; set; }
  104. [Column(Name = "inserted", DbType = "datetimeoffset", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  105. public DateTimeOffset Inserted { get; set; }
  106. [Column(Name = "maxexecutioncount", DbType = "int", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  107. public int MaxExecutionCount { get; set; }
  108. [Column(Name = "executioncount", DbType = "int", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  109. public int ExecutionCount { get; set; }
  110. [Column(Name = "authorisedapps", DbType = "varchar(2048)", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
  111. public string AuthorisedApps { get; set; }
  112. [Column(Name = "invitecode", DbType = "varchar(36)", UpdateCheck = UpdateCheck.Never)]
  113. public string InviteCode { get; set; }
  114. [Column(Name = "emailaddressconfirmed", DbType = "bit", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
  115. public bool EmailAddressConfirmed { get; set; }
  116. public Customer() { }
  117. public static string ValidateAndClean(Customer customer) {
  118. if (customer.FirstName.IsNullOrBlank()) {
  119. return "A first name must be specified.";
  120. }
  121. else if (customer.FirstName.Trim().Length > MAX_FIELD_LENGTH) {
  122. return "The first name length must be less than " + MAX_FIELD_LENGTH + ".";
  123. }
  124. else if (customer.LastName.IsNullOrBlank()) {
  125. return "A last name must be specified.";
  126. }
  127. else if (customer.LastName.Trim().Length > MAX_FIELD_LENGTH) {
  128. return "The last name length must be less than " + MAX_FIELD_LENGTH + ".";
  129. }
  130. else if (customer.EmailAddress.IsNullOrBlank()) {
  131. return "An email address must be specified.";
  132. }
  133. else if (customer.EmailAddress.Trim().Length > MAX_FIELD_LENGTH) {
  134. return "The email address length must be less than " + MAX_FIELD_LENGTH + ".";
  135. }
  136. else if (!Regex.Match(customer.EmailAddress, @"\S+@\S+\.\S+").Success) {
  137. return "The email address was an invalid format.";
  138. }
  139. else if (customer.CustomerUsername.IsNullOrBlank()) {
  140. return "A username must be specified.";
  141. }
  142. else if (customer.CustomerUsername.Trim().Length > MAX_USERNAME_LENGTH || customer.CustomerUsername.Trim().Length < MIN_USERNAME_LENGTH) {
  143. return "The username length must be between " + MIN_USERNAME_LENGTH + " and " + MAX_USERNAME_LENGTH + ".";
  144. }
  145. else if (Regex.Match(customer.CustomerUsername.Trim(), "[^" + USERNAME_ALLOWED_CHARS + "]").Success) {
  146. return "The username had an invalid character, characters permitted are alpha-numeric and -_ (no full stop characters '.' are allowed).";
  147. }
  148. else if (customer.CustomerPassword.IsNullOrBlank()) {
  149. return "A password must be specified.";
  150. }
  151. else if (customer.CustomerPassword.Trim().Length > MAX_PASSWORD_LENGTH || customer.CustomerPassword.Trim().Length < MIN_PASSWORD_LENGTH) {
  152. return "The password length must be between " + MIN_PASSWORD_LENGTH + " and " + MAX_PASSWORD_LENGTH + ".";
  153. }
  154. else if (customer.SecurityAnswer.IsNullOrBlank()) {
  155. return "The answer to the security question must be specified.";
  156. }
  157. else if (customer.SecurityAnswer.Trim().Length > MAX_FIELD_LENGTH) {
  158. return "The security question answer length must be less than " + MAX_FIELD_LENGTH + ".";
  159. }
  160. else if (customer.City.IsNullOrBlank()) {
  161. return "Your city must be specified. If you don't live in a city please enter the one closest to you.";
  162. }
  163. else if (customer.City.Trim().Length > MAX_FIELD_LENGTH) {
  164. return "The city length must be less than " + MAX_FIELD_LENGTH + ".";
  165. }
  166. else if (!customer.WebSite.IsNullOrBlank() && customer.WebSite.Trim().Length > MAX_WEBSITE_FIELD_LENGTH) {
  167. return "The web site length must be less than " + MAX_WEBSITE_FIELD_LENGTH + ".";
  168. }
  169. return null;
  170. }
  171. #if !SILVERLIGHT
  172. public Customer(DataRow customerRow) {
  173. Load(customerRow);
  174. }
  175. public DataTable GetTable() {
  176. DataTable table = new DataTable();
  177. table.Columns.Add(new DataColumn("id", typeof(String)));
  178. table.Columns.Add(new DataColumn("customerusername", typeof(String)));
  179. table.Columns.Add(new DataColumn("customerpassword", typeof(String)));
  180. table.Columns.Add(new DataColumn("emailaddress", typeof(String)));
  181. table.Columns.Add(new DataColumn("adminid", typeof(String)));
  182. table.Columns.Add(new DataColumn("adminmemberid", typeof(String)));
  183. table.Columns.Add(new DataColumn("firstname", typeof(String)));
  184. table.Columns.Add(new DataColumn("lastname", typeof(String)));
  185. table.Columns.Add(new DataColumn("city", typeof(String)));
  186. table.Columns.Add(new DataColumn("country", typeof(String)));
  187. table.Columns.Add(new DataColumn("securityquestion", typeof(String)));
  188. table.Columns.Add(new DataColumn("securityanswer", typeof(String)));
  189. table.Columns.Add(new DataColumn("website", typeof(String)));
  190. table.Columns.Add(new DataColumn("createdfromipaddress", typeof(String)));
  191. table.Columns.Add(new DataColumn("inserted", typeof(DateTime)));
  192. table.Columns.Add(new DataColumn("active", typeof(Boolean)));
  193. table.Columns.Add(new DataColumn("suspended", typeof(Boolean)));
  194. table.Columns.Add(new DataColumn("suspendedreason", typeof(String)));
  195. table.Columns.Add(new DataColumn("ipaddress", typeof(String)));
  196. table.Columns.Add(new DataColumn("executioncount", typeof(Int32)));
  197. table.Columns.Add(new DataColumn("maxexecutioncount", typeof(Int32)));
  198. table.Columns.Add(new DataColumn("authorisedapps", typeof(String)));
  199. table.Columns.Add(new DataColumn("timezone", typeof(String)));
  200. table.Columns.Add(new DataColumn("invitecode", typeof(String)));
  201. table.Columns.Add(new DataColumn("emailaddressconfirmed", typeof(Boolean)));
  202. return table;
  203. }
  204. public void Load(DataRow customerRow) {
  205. try {
  206. Id = new Guid(customerRow["id"] as string);
  207. CustomerUsername = customerRow["customerusername"] as string;
  208. CustomerPassword = customerRow["customerpassword"] as string;
  209. EmailAddress = (customerRow.Table.Columns.Contains("emailaddress") && customerRow["emailaddress"] != null) ? customerRow["emailaddress"] as string : null;
  210. AdminId = (customerRow.Table.Columns.Contains("adminid") && customerRow["adminid"] != null) ? customerRow["adminid"] as string : null;
  211. AdminMemberId = (customerRow.Table.Columns.Contains("adminmemberid") && customerRow["adminmemberid"] != null) ? customerRow["adminmemberid"] as string : null;
  212. FirstName = (customerRow.Table.Columns.Contains("firstname") && customerRow["firstname"] != null) ? customerRow["firstname"] as string : null;
  213. LastName = (customerRow.Table.Columns.Contains("lastname") && customerRow["lastname"] != null) ? customerRow["lastname"] as string : null;
  214. City = (customerRow.Table.Columns.Contains("city") && customerRow["city"] != null) ? customerRow["city"] as string : null;
  215. Country = (customerRow.Table.Columns.Contains("country") && customerRow["country"] != null) ? customerRow["country"] as string : null;
  216. SecurityQuestion = (customerRow.Table.Columns.Contains("securityquestion") && customerRow["securityquestion"] != null) ? customerRow["securityquestion"] as string : null;
  217. SecurityAnswer = (customerRow.Table.Columns.Contains("securityanswer") && customerRow["securityanswer"] != null) ? customerRow["securityanswer"] as string : null;
  218. WebSite = (customerRow.Table.Columns.Contains("website") && customerRow["website"] != null) ? customerRow["website"] as string : null;
  219. CreatedFromIPAddress = (customerRow.Table.Columns.Contains("createdfromipaddress") && customerRow["createdfromipaddress"] != null) ? customerRow["createdfromipaddress"] as string : null;
  220. Inserted = (customerRow.Table.Columns.Contains("inserted") && customerRow["inserted"] != null) ? DateTimeOffset.Parse(customerRow["inserted"] as string) : DateTimeOffset.MinValue;
  221. Suspended = (customerRow.Table.Columns.Contains("suspended") && customerRow["suspended"] != null) ? Convert.ToBoolean(customerRow["suspended"]) : false;
  222. SuspendedReason = (customerRow.Table.Columns.Contains("suspendedreason") && customerRow["suspendedreason"] != null) ? customerRow["suspendedreason"] as string : null;
  223. ExecutionCount = (customerRow.Table.Columns.Contains("executioncount") && customerRow["executioncount"] != null) ? Convert.ToInt32(customerRow["executioncount"]) : 0;
  224. MaxExecutionCount = (customerRow.Table.Columns.Contains("maxexecutioncount") && customerRow["maxexecutioncount"] != null) ? Convert.ToInt32(customerRow["maxexecutioncount"]) : DEFAULT_MAXIMUM_EXECUTION_COUNT;
  225. AuthorisedApps = (customerRow.Table.Columns.Contains("authorisedapps") && customerRow["authorisedapps"] != null) ? customerRow["authorisedapps"] as string : null;
  226. EmailAddressConfirmed = (customerRow.Table.Columns.Contains("emailaddressconfirmed") && customerRow["emailaddressconfirmed"] != null) ? Convert.ToBoolean(customerRow["emailaddressconfirmed"]) : true;
  227. InviteCode = (customerRow.Table.Columns.Contains("invitecode") && customerRow["invitecode"] != null) ? customerRow["invitecode"] as string : null;
  228. TimeZone = (customerRow.Table.Columns.Contains("timezone") && customerRow["timezone"] != null) ? customerRow["timezone"] as string : null;
  229. }
  230. catch (Exception excp) {
  231. logger.Error("Exception Customer Load. " + excp.Message);
  232. throw;
  233. }
  234. }
  235. public Dictionary<Guid, object> Load(XmlDocument dom) {
  236. return SIPAssetXMLPersistor<Customer>.LoadAssetsFromXMLRecordSet(dom);
  237. }
  238. #endif
  239. public string ToXML()
  240. {
  241. string customerXML =
  242. " <" + XML_ELEMENT_NAME + ">" + m_newLine +
  243. ToXMLNoParent() + m_newLine +
  244. " </" + XML_ELEMENT_NAME + ">" + m_newLine;
  245. return customerXML;
  246. }
  247. public string ToXMLNoParent() {
  248. string customerXML =
  249. " <id>" + Id + "</id>" + m_newLine +
  250. " <customerusername>" + CustomerUsername + "</customerusername>" + m_newLine +
  251. " <customerpassword>" + CustomerPassword + "</customerpassword>" + m_newLine +
  252. " <emailaddress>" + EmailAddress + "</emailaddress>" + m_newLine +
  253. " <firstname>" + SafeXML.MakeSafeXML(FirstName) + "</firstname>" + m_newLine +
  254. " <lastname>" + SafeXML.MakeSafeXML(LastName) + "</lastname>" + m_newLine +
  255. " <city>" + SafeXML.MakeSafeXML(City) + "</city>" + m_newLine +
  256. " <country>" + Country + "</country>" + m_newLine +
  257. " <adminid>" + AdminId + "</adminid>" + m_newLine +
  258. " <adminmemberid>" + AdminMemberId + "</adminmemberid>" + m_newLine +
  259. " <website>" + SafeXML.MakeSafeXML(WebSite) + "</website>" + m_newLine +
  260. " <securityquestion>" + SecurityQuestion + "</securityquestion>" + m_newLine +
  261. " <securityanswer>" + SafeXML.MakeSafeXML(SecurityAnswer) + "</securityanswer>" + m_newLine +
  262. " <createdfromipaddress>" + CreatedFromIPAddress + "</createdfromipaddress>" + m_newLine +
  263. " <inserted>" + Inserted.ToString() + "</inserted>" + m_newLine +
  264. " <active>" + Active + "</active>" + m_newLine +
  265. " <suspended>" + Suspended + "</suspended>" + m_newLine +
  266. " <suspendedreason>" + SuspendedReason + "</suspendedreason>" + m_newLine +
  267. " <executioncount>" + ExecutionCount + "</executioncount>" + m_newLine +
  268. " <maxexecutioncount>" + MaxExecutionCount + "</maxexecutioncount>" + m_newLine +
  269. " <authorisedapps>" + AuthorisedApps + "</authorisedapps>" + m_newLine +
  270. " <invitecode>" + InviteCode + "</invitecode>" + m_newLine +
  271. " <emailaddressconfirmed>" + EmailAddressConfirmed + "</emailaddressconfirmed>";
  272. return customerXML;
  273. }
  274. public string GetXMLElementName() {
  275. return XML_ELEMENT_NAME;
  276. }
  277. public string GetXMLDocumentElementName() {
  278. return XML_DOCUMENT_ELEMENT_NAME;
  279. }
  280. }
  281. }