Skip to content

Commit

Permalink
Rights structure protptype
Browse files Browse the repository at this point in the history
  • Loading branch information
Splamy committed Jun 15, 2017
1 parent ce80c16 commit 8a5400a
Show file tree
Hide file tree
Showing 9 changed files with 651 additions and 13 deletions.
5 changes: 4 additions & 1 deletion TS3AudioBot/MainBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace TS3AudioBot
using Sessions;
using Web.Api;
using Web;
using Rights;

using TS3Client;
using TS3Client.Messages;
Expand Down Expand Up @@ -75,6 +76,7 @@ internal static void Main(string[] args)
public PlayManager PlayManager { get; private set; }
public ITargetManager TargetManager { get; private set; }
public ConfigFile ConfigManager { get; private set; }
public RightsManager RightsManager { get; private set; }

public bool QuizMode { get; set; }

Expand Down Expand Up @@ -116,6 +118,7 @@ private bool InitializeBot()
var pld = ConfigManager.GetDataStruct<PlaylistManagerData>("PlaylistManager", true);
var yfd = ConfigManager.GetDataStruct<YoutubeFactoryData>("YoutubeFactory", true);
var webd = ConfigManager.GetDataStruct<WebData>("WebData", true);
var rmd = ConfigManager.GetDataStruct<RightsManagerData>("RightsManager", true);
mainBotData = ConfigManager.GetDataStruct<MainBotData>("MainBot", true);
ConfigManager.Close();

Expand Down Expand Up @@ -161,6 +164,7 @@ private bool InitializeBot()
PluginManager = new PluginManager(this, pmd);
PlayManager = new PlayManager(this);
WebManager = new WebManager(this, webd);
RightsManager = new RightsManager(rmd);
TargetManager = teamspeakClient;

Log.Write(Log.Level.Info, "[=========== Initializing Factories ===========]");
Expand Down Expand Up @@ -194,7 +198,6 @@ private bool InitializeBot()
// Register callback to remove open private sessions, when user disconnects
//QueryConnection.OnClientDisconnect += (s, e) => SessionManager.RemoveSession(e.InvokerUid);


Log.Write(Log.Level.Info, "[================= Finalizing =================]");
WebManager.StartServerAsync();

Expand Down
9 changes: 5 additions & 4 deletions TS3AudioBot/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand All @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TS3AudioBot")]
[assembly: AssemblyCopyright("Copyright © Splamy 2016")]
[assembly: AssemblyCopyright("Copyright © Splamy 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -31,5 +31,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("0.2.*")]
[assembly: AssemblyFileVersion("0.2.*")]
[assembly: AssemblyInformationalVersion("0.2.0")]
102 changes: 102 additions & 0 deletions TS3AudioBot/Rights/RightsDecl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// TS3AudioBot - An advanced Musicbot for Teamspeak 3
// Copyright (C) 2016 TS3AudioBot contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

namespace TS3AudioBot.Rights
{
using Nett;
using System.Collections.Generic;
using System.Linq;

internal abstract class RightsDecl
{
public int Id { get; private set; }
public int Level { get; set; }

public RightsRule Parent { get; set; }
private string[] includeNames;
public RightsGroup[] Includes { get; set; }

public string[] DeclAdd { get; set; }
public string[] DeclDeny { get; set; }

public RightsDecl() { }

public virtual void FillNull()
{
if (includeNames == null) includeNames = new string[0];
if (DeclAdd == null) DeclAdd = new string[0];
if (DeclDeny == null) DeclDeny = new string[0];
}

public virtual bool ParseKey(string key, TomlObject tomlObj, List<RightsDecl> rules)
{
switch (key)
{
case "+":
DeclAdd = TomlTools.GetValues<string>(tomlObj);
if (DeclAdd == null)
Log.Write(Log.Level.Error, "<+> Field has invalid data.");
break;
case "-":
DeclDeny = TomlTools.GetValues<string>(tomlObj);
if (DeclDeny == null)
Log.Write(Log.Level.Error, "<-> Field has invalid data.");
break;
case "include":
includeNames = TomlTools.GetValues<string>(tomlObj);
if (includeNames == null)
Log.Write(Log.Level.Error, "<include> Field has invalid data.");
break;
default: return false;
}
return true;
}

public void ParseChilden(TomlTable tomlObj, List<RightsDecl> rules)
{
Id = rules.Count;
rules.Add(this);

foreach (var item in tomlObj)
{
if (!ParseKey(item.Key, item.Value, rules))
{
Log.Write(Log.Level.Error, "Unrecognized key <{0}>.", item.Key);
}
}
FillNull();
}

public abstract RightsGroup ResolveGroup(string groupName);

public bool ResolveIncludes()
{
bool hasErrors = false;
if (includeNames != null)
{
Includes = includeNames.Select(ResolveGroup).ToArray();
for (int i = 0; i < includeNames.Length; i++)
if (Includes[i] == null)
{
Log.Write(Log.Level.Error, "Could not find group \"{0}\" to include.", includeNames[i]);
hasErrors = true;
}
includeNames = null;
}
return !hasErrors;
}
}
}
37 changes: 37 additions & 0 deletions TS3AudioBot/Rights/RightsGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// TS3AudioBot - An advanced Musicbot for Teamspeak 3
// Copyright (C) 2016 TS3AudioBot contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

namespace TS3AudioBot.Rights
{
internal class RightsGroup : RightsDecl
{
public string Name { get; }

public RightsGroup(string name)
{
Name = name;
}

public override RightsGroup ResolveGroup(string groupName)
{
if (Name == groupName)
return this;
if (Parent == null)
return null;
return Parent.ResolveGroup(groupName);
}
}
}
Loading

0 comments on commit 8a5400a

Please sign in to comment.