/src/Procon.Core/Interfaces/Security/Objects/LocalAccount.cs

# · C# · 199 lines · 118 code · 25 blank · 56 comment · 38 complexity · b885b83ee802d586dfad9ee1afe29454 MD5 · raw file

  1. // Copyright 2011 Geoffrey 'Phogue' Green
  2. //
  3. // http://www.phogue.net
  4. //
  5. // This file is part of Procon 2.
  6. //
  7. // Procon 2 is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // Procon 2 is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with Procon 2. If not, see <http://www.gnu.org/licenses/>.
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Xml.Linq;
  24. using Newtonsoft.Json;
  25. using System.IO;
  26. namespace Procon.Core.Interfaces.Security.Objects {
  27. using Procon.Core.Utils;
  28. using Procon.Net.Protocols;
  29. public class LocalAccount : Account {
  30. [JsonIgnore]
  31. public ISecurityController Security { get; set; }
  32. public override Account Execute() {
  33. this.Config = new Config() {
  34. DirectoryInfo = new DirectoryInfo(Path.Combine(Defines.CONFIGS_ACCOUNTS_DIRECTORY, PathValidator.Valdiate(this.Username)))
  35. };
  36. return base.Execute();
  37. }
  38. protected override void AssignEvents() {
  39. this.Assignments.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Assignments_CollectionChanged);
  40. base.AssignEvents();
  41. }
  42. private void Assignments_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
  43. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) {
  44. foreach (Account item in e.NewItems) {
  45. this.Layer.Request(
  46. new Layer.Objects.Context() {
  47. ContextType = Interfaces.Layer.Objects.ContextType.All
  48. },
  49. CommandName.None,
  50. EventName.SecurityAccountsUidAssigned,
  51. item
  52. );
  53. }
  54. this.OnPropertyChanged(this, "Assignments");
  55. }
  56. else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) {
  57. foreach (Account item in e.OldItems) {
  58. this.Layer.Request(
  59. new Layer.Objects.Context() {
  60. ContextType = Interfaces.Layer.Objects.ContextType.All
  61. },
  62. CommandName.None,
  63. EventName.SecurityAccountsUidRevoked,
  64. item
  65. );
  66. }
  67. this.OnPropertyChanged(this, "Assignments");
  68. }
  69. }
  70. // TODO: Now that the flat files are gone this no longer requires a username parameter.
  71. [Command(Command = CommandName.SecurityAccountsAssign)]
  72. public override void Assign(CommandInitiator initiator, string username, GameType gameType, string uid) {
  73. if (initiator.CommandOrigin == CommandOrigin.Remote && this.Security.Can(this.Security.Account(initiator.Username), initiator.Command, this.Security.Account(username)) == false) {
  74. return;
  75. }
  76. if (this.Username == username) {
  77. if (this.Assignments.Where(x => x.GameType == gameType && x.UID == uid).FirstOrDefault() == null) {
  78. this.Assignments.Add(
  79. new AccountAssignment() {
  80. GameType = gameType,
  81. UID = uid
  82. }
  83. );
  84. }
  85. }
  86. }
  87. // TODO: Now that the flat files are gone this no longer requires a username parameter.
  88. [Command(Command = CommandName.SecurityAccountsRevoke)]
  89. public override void Revoke(CommandInitiator initiator, string username, GameType gameType, string uid) {
  90. if (initiator.CommandOrigin == CommandOrigin.Remote && this.Security.Can(this.Security.Account(initiator.Username), initiator.Command, this.Security.Account(username)) == false) {
  91. return;
  92. }
  93. if (this.Username == username) {
  94. this.Assignments.RemoveAll(x => x.GameType == gameType && x.UID == uid);
  95. }
  96. }
  97. // TODO: Now that the flat files are gone this no longer requires a username parameter.
  98. [Command(Command = CommandName.SecurityAccountsSetPassword)]
  99. public override void SetPassword(CommandInitiator initiator, string username, string password) {
  100. if (initiator.CommandOrigin == CommandOrigin.Remote && this.Security.Can(this.Security.Account(initiator.Username), initiator.Command, this.Security.Account(username)) == false) {
  101. return;
  102. }
  103. if (this.Username == username && password.Length > 0) {
  104. this.Password = password;
  105. }
  106. }
  107. // TODO: Now that the flat files are gone this no longer requires a username parameter.
  108. [Command(Command = CommandName.SecurityAccountsSetPreferredLanguageCode)]
  109. public override void SetPreferredLanguageCode(CommandInitiator initiator, string username, string languageCode) {
  110. if (initiator.CommandOrigin == CommandOrigin.Remote && this.Security.Can(this.Security.Account(initiator.Username), initiator.Command, this.Security.Account(username)) == false) {
  111. return;
  112. }
  113. if (this.Username == username) {
  114. this.PreferredLanguageCode = languageCode;
  115. this.OnPropertyChanged(this, "PreferredLanguageCode");
  116. }
  117. }
  118. protected override void WriteConfig(XElement xNamespace) {
  119. base.WriteConfig(xNamespace);
  120. xNamespace.Add(new XElement("command",
  121. new XAttribute("name", CommandName.SecurityAccountsSetPassword),
  122. new XElement("username", this.Username),
  123. new XElement("password", this.Password)
  124. ));
  125. xNamespace.Add(new XElement("command",
  126. new XAttribute("name", CommandName.SecurityAccountsSetPreferredLanguageCode),
  127. new XElement("username", this.Username),
  128. new XElement("languageCode", this.PreferredLanguageCode)
  129. ));
  130. foreach (AccountAssignment assignment in this.Assignments) {
  131. xNamespace.Add(new XElement("command",
  132. new XAttribute("name", CommandName.SecurityAccountsAssign),
  133. new XElement("username", this.Username),
  134. new XElement("gameType", assignment.GameType),
  135. new XElement("uid", assignment.UID)
  136. ));
  137. }
  138. }
  139. /*
  140. public override List<List<object>> ToWords() {
  141. List<List<object>> words = new List<List<object>>();
  142. words.Add(
  143. new List<object>() {
  144. CommandName.SecurityAccountsSetPassword,
  145. this.Username,
  146. this.Password
  147. }
  148. );
  149. words.Add(
  150. new List<object>() {
  151. CommandName.SecurityAccountsSetPreferredLanguageCode,
  152. this.Username,
  153. this.PreferredLanguageCode
  154. }
  155. );
  156. foreach (AccountAssignment assignment in this.Assignments) {
  157. words.Add(
  158. new List<object>() {
  159. CommandName.SecurityAccountsAssign,
  160. this.Username,
  161. assignment.GameType,
  162. assignment.UID
  163. }
  164. );
  165. }
  166. return words;
  167. }
  168. */
  169. }
  170. }