/src/Procon.Core/Interfaces/Security/Objects/LocalAccount.cs
# · C# · 199 lines · 118 code · 25 blank · 56 comment · 38 complexity · b885b83ee802d586dfad9ee1afe29454 MD5 · raw file
- // Copyright 2011 Geoffrey 'Phogue' Green
- //
- // http://www.phogue.net
- //
- // This file is part of Procon 2.
- //
- // Procon 2 is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // Procon 2 is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with Procon 2. If not, see <http://www.gnu.org/licenses/>.
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml.Linq;
- using Newtonsoft.Json;
- using System.IO;
-
- namespace Procon.Core.Interfaces.Security.Objects {
- using Procon.Core.Utils;
- using Procon.Net.Protocols;
-
- public class LocalAccount : Account {
-
- [JsonIgnore]
- public ISecurityController Security { get; set; }
-
- public override Account Execute() {
- this.Config = new Config() {
- DirectoryInfo = new DirectoryInfo(Path.Combine(Defines.CONFIGS_ACCOUNTS_DIRECTORY, PathValidator.Valdiate(this.Username)))
- };
-
- return base.Execute();
- }
-
- protected override void AssignEvents() {
- this.Assignments.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Assignments_CollectionChanged);
-
- base.AssignEvents();
- }
-
- private void Assignments_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
- if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) {
- foreach (Account item in e.NewItems) {
- this.Layer.Request(
- new Layer.Objects.Context() {
- ContextType = Interfaces.Layer.Objects.ContextType.All
- },
- CommandName.None,
- EventName.SecurityAccountsUidAssigned,
- item
- );
- }
-
- this.OnPropertyChanged(this, "Assignments");
- }
- else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) {
- foreach (Account item in e.OldItems) {
- this.Layer.Request(
- new Layer.Objects.Context() {
- ContextType = Interfaces.Layer.Objects.ContextType.All
- },
- CommandName.None,
- EventName.SecurityAccountsUidRevoked,
- item
- );
- }
-
- this.OnPropertyChanged(this, "Assignments");
- }
- }
-
- // TODO: Now that the flat files are gone this no longer requires a username parameter.
- [Command(Command = CommandName.SecurityAccountsAssign)]
- public override void Assign(CommandInitiator initiator, string username, GameType gameType, string uid) {
- if (initiator.CommandOrigin == CommandOrigin.Remote && this.Security.Can(this.Security.Account(initiator.Username), initiator.Command, this.Security.Account(username)) == false) {
- return;
- }
-
- if (this.Username == username) {
- if (this.Assignments.Where(x => x.GameType == gameType && x.UID == uid).FirstOrDefault() == null) {
- this.Assignments.Add(
- new AccountAssignment() {
- GameType = gameType,
- UID = uid
- }
- );
- }
- }
- }
-
- // TODO: Now that the flat files are gone this no longer requires a username parameter.
- [Command(Command = CommandName.SecurityAccountsRevoke)]
- public override void Revoke(CommandInitiator initiator, string username, GameType gameType, string uid) {
- if (initiator.CommandOrigin == CommandOrigin.Remote && this.Security.Can(this.Security.Account(initiator.Username), initiator.Command, this.Security.Account(username)) == false) {
- return;
- }
-
- if (this.Username == username) {
- this.Assignments.RemoveAll(x => x.GameType == gameType && x.UID == uid);
- }
- }
-
- // TODO: Now that the flat files are gone this no longer requires a username parameter.
- [Command(Command = CommandName.SecurityAccountsSetPassword)]
- public override void SetPassword(CommandInitiator initiator, string username, string password) {
- if (initiator.CommandOrigin == CommandOrigin.Remote && this.Security.Can(this.Security.Account(initiator.Username), initiator.Command, this.Security.Account(username)) == false) {
- return;
- }
-
- if (this.Username == username && password.Length > 0) {
- this.Password = password;
- }
- }
-
- // TODO: Now that the flat files are gone this no longer requires a username parameter.
- [Command(Command = CommandName.SecurityAccountsSetPreferredLanguageCode)]
- public override void SetPreferredLanguageCode(CommandInitiator initiator, string username, string languageCode) {
- if (initiator.CommandOrigin == CommandOrigin.Remote && this.Security.Can(this.Security.Account(initiator.Username), initiator.Command, this.Security.Account(username)) == false) {
- return;
- }
-
- if (this.Username == username) {
- this.PreferredLanguageCode = languageCode;
-
- this.OnPropertyChanged(this, "PreferredLanguageCode");
- }
- }
-
- protected override void WriteConfig(XElement xNamespace) {
- base.WriteConfig(xNamespace);
-
- xNamespace.Add(new XElement("command",
- new XAttribute("name", CommandName.SecurityAccountsSetPassword),
- new XElement("username", this.Username),
- new XElement("password", this.Password)
- ));
-
- xNamespace.Add(new XElement("command",
- new XAttribute("name", CommandName.SecurityAccountsSetPreferredLanguageCode),
- new XElement("username", this.Username),
- new XElement("languageCode", this.PreferredLanguageCode)
- ));
-
- foreach (AccountAssignment assignment in this.Assignments) {
- xNamespace.Add(new XElement("command",
- new XAttribute("name", CommandName.SecurityAccountsAssign),
- new XElement("username", this.Username),
- new XElement("gameType", assignment.GameType),
- new XElement("uid", assignment.UID)
- ));
- }
- }
-
- /*
- public override List<List<object>> ToWords() {
- List<List<object>> words = new List<List<object>>();
-
- words.Add(
- new List<object>() {
- CommandName.SecurityAccountsSetPassword,
- this.Username,
- this.Password
- }
- );
-
- words.Add(
- new List<object>() {
- CommandName.SecurityAccountsSetPreferredLanguageCode,
- this.Username,
- this.PreferredLanguageCode
- }
- );
-
- foreach (AccountAssignment assignment in this.Assignments) {
- words.Add(
- new List<object>() {
- CommandName.SecurityAccountsAssign,
- this.Username,
- assignment.GameType,
- assignment.UID
- }
- );
- }
-
- return words;
- }
- */
- }
- }