Create LevelAccess class, for a more consistent and modular way of controlling visit/build access permissions.

This commit is contained in:
UnknownShadow200 2016-08-24 12:30:13 +10:00
parent 0436384446
commit 060e48487d
4 changed files with 106 additions and 10 deletions

View File

@ -141,6 +141,8 @@ namespace MCGalaxy {
public bool Buildable = true;
[ConfigBool("Deletable", "Permissions", null, true)]
public bool Deletable = true;
public LevelAccess VisitAccess, BuildAccess;
[ConfigPerm("PerBuildMax", "Permissions", null, LevelPermission.Nobody, true)]
public LevelPermission perbuildmax = LevelPermission.Nobody;
[ConfigPerm("PerBuild", "Permissions", null, LevelPermission.Guest, true)]
@ -149,13 +151,16 @@ namespace MCGalaxy {
[ConfigPerm("PerVisit", "Permissions", null, LevelPermission.Guest, true)]
public LevelPermission permissionvisit = LevelPermission.Guest;
[ConfigPerm("PerVisitMax", "Permissions", null, LevelPermission.Nobody, true)]
public LevelPermission pervisitmax = LevelPermission.Nobody;
public LevelPermission pervisitmax = LevelPermission.Nobody;
// Other blacklists/whitelists
[ConfigStringList("VisitWhitelist", "Permissions", null)]
public List<string> VisitWhitelist = new List<string>();
[ConfigStringList("VisitBlacklist", "Permissions", null)]
public List<string> VisitBlacklist = new List<string>();
[ConfigStringList("BuildWhitelist", "Permissions", null)]
public List<string> BuildWhitelist = new List<string>();
[ConfigStringList("BuildBlacklist", "Permissions", null)]
public List<string> BuildBlacklist = new List<string>();
// Physics fields and settings
public int physics {

View File

@ -60,6 +60,9 @@ namespace MCGalaxy {
if (Height < 16) Height = 16;
if (Length < 16) Length = 16;
VisitAccess = new LevelAccess(this, true);
BuildAccess = new LevelAccess(this, false);
#pragma warning disable 0612
width = Width;
length = Height;
@ -136,17 +139,11 @@ namespace MCGalaxy {
public bool CanJoin(Player p) {
if (p == null) return true;
if (Player.BlacklistCheck(p.name, name) || VisitBlacklist.CaselessContains(p.name)) {
if (Player.BlacklistCheck(p.name, name)) {
Player.Message(p, "You are blacklisted from going to {0}.", name); return false;
}
bool whitelisted = VisitWhitelist.CaselessContains(p.name);
if (!p.ignorePermission && !whitelisted && p.Rank < permissionvisit) {
Player.Message(p, "You are not allowed to go to {0}.", name); return false;
}
if (!p.ignorePermission && !whitelisted && p.Rank > pervisitmax && !p.group.CanExecute("pervisitmax")) {
Player.Message(p, "Your rank must be ranked {1} or lower to go to {0}.", name, pervisitmax); return false;
}
if (!VisitAccess.CheckDetailed(p, p.ignorePermission)) return false;
if (File.Exists("text/lockdown/map/" + name)) {
Player.Message(p, "The level " + name + " is locked."); return false;
}

93
Levels/LevelAccess.cs Normal file
View File

@ -0,0 +1,93 @@
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
namespace MCGalaxy {
/// <summary> Encapuslates access permissions (visit or build) for a level. </summary>
public sealed class LevelAccess {
/// <summary> Whether these access permissions apply to
/// visit (true) or build (false) permission for the level. </summary>
public readonly bool IsVisit;
readonly Level lvl;
public LevelAccess(Level lvl, bool isVisit) {
this.lvl = lvl;
IsVisit = isVisit;
}
/// <summary> Lowest allowed rank. </summary>
public LevelPermission Min {
get { return IsVisit ? lvl.permissionvisit : lvl.permissionbuild; }
}
/// <summary> Highest allowed rank. </summary>
public LevelPermission Max {
get { return IsVisit ? lvl.pervisitmax : lvl.perbuildmax; }
}
/// <summary> List of always allowed players, overrides rank allowances. </summary>
public List<string> Whitelisted {
get { return IsVisit ? lvl.VisitWhitelist : lvl.BuildWhitelist; }
}
/// <summary> List of never allowed players, ignores rank allowances. </summary>
public List<string> Blacklisted {
get { return IsVisit ? lvl.VisitBlacklist : lvl.BuildBlacklist; }
}
/// <summary> Returns whether the given player is allowed by these access permissions. </summary>
public bool Check(Player p, bool ignoreRankPerm = false) {
if (Blacklisted.CaselessContains(p.name)) return false;
if (Whitelisted.CaselessContains(p.name) || ignoreRankPerm) return true;
if (p.Rank < Min) return false;
string maxCmd = IsVisit ? "pervisitmax" : "perbuildmax";
if (p.Rank > Max && !p.group.CanExecute(maxCmd)) return false;
return true;
}
/// <summary> Returns whether the given player is allowed for these access permissions. </summary>
/// <remarks> If the player is not allowed by these access permissions,
/// sends a message to the player describing why they are not. </remarks>
public bool CheckDetailed(Player p, bool ignoreRankPerm = false) {
string name = lvl.name;
string action = IsVisit ? "going to" : "building in";
if (Blacklisted.CaselessContains(p.name)) {
Player.Message(p, "You are blacklisted from {1} {0}.", name, action); return false;
}
if (Whitelisted.CaselessContains(p.name) || ignoreRankPerm) return true;
action = IsVisit? "go to" : "build in";
if (p.Rank < Min) {
Group grp = Group.findPerm(Min);
string grpName = grp == null ? "&f" + Min : grp.ColoredName;
Player.Message(p, "Only {2}%S+ may {1} {0}.", name, action, grpName); return false;
}
string maxCmd = IsVisit ? "pervisitmax" : "perbuildmax";
if (p.Rank > Max && !p.group.CanExecute(maxCmd)) {
Group grp = Group.findPerm(Max);
string grpName = grp == null ? "&f" + Max : grp.ColoredName;
Player.Message(p, "Only {2} and below may {1} in {0}.", name, action, grpName) return false;
}
}
}
}

View File

@ -497,6 +497,7 @@
<Compile Include="Levels\IO\McfFile.cs" />
<Compile Include="Levels\Level.Blocks.cs" />
<Compile Include="Levels\Level.Fields.cs" />
<Compile Include="Levels\LevelAccess.cs" />
<Compile Include="Levels\LevelDB.cs" />
<Compile Include="Levels\Level.Physics.cs" />
<Compile Include="Levels\LevelActions.cs" />