You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
417 lines
13 KiB
417 lines
13 KiB
/** |
|
* Command for displaying help information about registered Acedia's commands. |
|
* Copyright 2022 Anton Tarasenko |
|
*------------------------------------------------------------------------------ |
|
* This file is part of Acedia. |
|
* |
|
* Acedia 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, version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* Acedia 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 Acedia. If not, see <https://www.gnu.org/licenses/>. |
|
*/ |
|
class ACommandUserGroups extends Command |
|
dependson(Users_Feature); |
|
|
|
protected function BuildData(CommandDataBuilder builder) |
|
{ |
|
builder.Name(P("usergroups")) |
|
.Group(P("admin")) |
|
.Summary(P("User groups management.")) |
|
.Describe(P("Allows to add/remove user groups and users to these:" |
|
@ "groups. Changes made by it will always affect current session," |
|
@ "but might fail to be saved in case user groups are stored in" |
|
@ "a database that is either corrupted or in read-only mode.")); |
|
builder.SubCommand(P("show")) |
|
.Describe(P("Shows all groups along with users that belong to them.")); |
|
builder.SubCommand(P("add")) |
|
.Describe(P("Adds a new group")) |
|
.ParamText(P("group_name")); |
|
builder.SubCommand(P("remove")) |
|
.Describe(P("Removes a group")) |
|
.ParamText(P("group_name")); |
|
builder.SubCommand(P("adduser")) |
|
.Describe(P("Adds new user to the group. Allows to also optionally" |
|
@ "specify annotation (human-readable name) that can be thought of" |
|
@ "as a {$TextEmphasis comment}.")) |
|
.ParamText(P("group_name")) |
|
.ParamText(P("user_id")) |
|
.OptionalParams() |
|
.ParamText(P("annotation")); |
|
builder.SubCommand(P("removeuser")) |
|
.Describe(P("Removes user from the group. User can be specified by both" |
|
@ "user's id or annotation, with id taking priority.")) |
|
.ParamText(P("group_name")) |
|
.ParamText(P("user_name")); |
|
} |
|
|
|
protected function Executed(CallData arguments, EPlayer instigator) |
|
{ |
|
local Text groupName, userID, userName, annotation; |
|
|
|
groupName = arguments.parameters.GetText(P("group_name")); |
|
// For parameters named `user_id`, can only be ID |
|
userID = arguments.parameters.GetText(P("user_id")); |
|
// For parameters named `user_id`, can be either ID or annotation |
|
userName = arguments.parameters.GetText(P("user_name")); |
|
annotation = arguments.parameters.GetText(P("annotation")); |
|
if (arguments.subCommandName.IsEmpty()) { |
|
DisplayUserGroups(); |
|
} |
|
else if (arguments.subCommandName.Compare(P("show"), SCASE_SENSITIVE)) { |
|
DisplayUserGroupsWithUsers(); |
|
} |
|
else if (arguments.subCommandName.Compare(P("add"), SCASE_SENSITIVE)) { |
|
AddGroup(groupName); |
|
} |
|
else if (arguments.subCommandName.Compare(P("remove"), SCASE_SENSITIVE)) { |
|
RemoveGroup(groupName); |
|
} |
|
else if (arguments.subCommandName.Compare(P("adduser"), SCASE_SENSITIVE)) { |
|
AddUser(groupName, userID, annotation); |
|
} |
|
else if (arguments.subCommandName.Compare(P("removeuser"), SCASE_SENSITIVE)) |
|
{ |
|
RemoveUser(groupName, userName); |
|
} |
|
_.memory.Free(groupName); |
|
_.memory.Free(userID); |
|
_.memory.Free(userName); |
|
_.memory.Free(annotation); |
|
} |
|
|
|
private function AddUser( |
|
BaseText groupName, |
|
BaseText textUserID, |
|
BaseText annotation) |
|
{ |
|
local bool userInGroup; |
|
local UserID id; |
|
|
|
if (groupName == none) return; |
|
if (textUserID == none) return; |
|
|
|
id = UserID(_.memory.Allocate(class'UserID')); |
|
id.Initialize(textUserID); |
|
if (_.users.IsUserIDInGroup(id, groupName)) |
|
{ |
|
userInGroup = true; |
|
callerConsole |
|
.Write(P("User ")) |
|
.UseColorOnce(_.color.Gray) |
|
.Write(textUserID) |
|
.UseColorOnce(_.color.TextFailure) |
|
.Write(P(" is already in the group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.WriteLine(P("!")); |
|
} |
|
else if (_.users.AddUserIDToGroup(id, groupName)) |
|
{ |
|
userInGroup = true; |
|
callerConsole |
|
.Write(F("{$TextPositive Added} user ")) |
|
.UseColorOnce(_.color.Gray) |
|
.Write(textUserID) |
|
.Write(P(" to the group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.WriteLine(P("!")); |
|
} |
|
else { |
|
// One of the reasons - NO GROUP |
|
callerConsole |
|
.UseColorOnce(_.color.TextFailure) |
|
.Write(P("Failed (for unknown reason)")) |
|
.Write(P(" to add user ")) |
|
.UseColorOnce(_.color.Gray) |
|
.Write(textUserID) |
|
.Write(P(" to the group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.WriteLine(P("!")); |
|
} |
|
if (!userInGroup || annotation == none) { |
|
return; |
|
} |
|
_.users.SetAnnotationForUserID(groupName, id, annotation); |
|
_.memory.Free(id); |
|
callerConsole |
|
.Write(P("Annotation for user ")) |
|
.UseColorOnce(_.color.Gray) |
|
.Write(textUserID) |
|
.UseColorOnce(_.color.TextPositive) |
|
.Write(P(" in the group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.Write(P(" is set to ")) |
|
.UseColorOnce(_.color.TextNeutral) |
|
.WriteLine(annotation); |
|
} |
|
|
|
private function RemoveUser(BaseText groupName, BaseText userName) |
|
{ |
|
local int i; |
|
local UserID idFromName, idToRemove; |
|
local array<Users_Feature.AnnotatedUserID> annotatedUsers; |
|
|
|
if (groupName == none) return; |
|
if (userName == none) return; |
|
|
|
idFromName = UserID(_.memory.Allocate(class'UserID')); |
|
idFromName.Initialize(userName); |
|
annotatedUsers = _.users.GetAnnotatedGroupMembers(groupName); |
|
if (idFromName.IsInitialized()) |
|
{ |
|
for (i = 0; i < annotatedUsers.length; i += 1) |
|
{ |
|
if (idFromName.IsEqual(annotatedUsers[i].id)) |
|
{ |
|
idToRemove = annotatedUsers[i].id; |
|
break; |
|
} |
|
} |
|
} |
|
_.memory.Free(idFromName); |
|
if (idToRemove == none) |
|
{ |
|
for (i = 0; i < annotatedUsers.length; i += 1) |
|
{ |
|
if (userName.Compare( |
|
annotatedUsers[i].annotation, |
|
SCASE_INSENSITIVE)) |
|
{ |
|
idToRemove = annotatedUsers[i].id; |
|
break; |
|
} |
|
} |
|
} |
|
if (idToRemove == none) |
|
{ |
|
callerConsole |
|
.Write(P("User ")) |
|
.UseColorOnce(_.color.Gray) |
|
.Write(userName) |
|
.UseColorOnce(_.color.TextFailure) |
|
.Write(P(" doesn't belong to the group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.WriteLine(P("!")); |
|
} |
|
else if (_.users.RemoveUserIDFromGroup(idToRemove, groupName)) |
|
{ |
|
callerConsole |
|
.Write(F("{$TextNegative Removed} user ")) |
|
.UseColorOnce(_.color.Gray) |
|
.Write(userName) |
|
.Write(P(" from the group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.WriteLine(P("!")); |
|
} |
|
else { |
|
callerConsole |
|
.UseColorOnce(_.color.TextFailure) |
|
.Write(P("Failed (for unknown reason)")) |
|
.Write(P("to remove user ")) |
|
.UseColorOnce(_.color.Gray) |
|
.Write(userName) |
|
.Write(P(" from the group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.WriteLine(P(".")); |
|
} |
|
for (i = 0; i < annotatedUsers.length; i += 1) |
|
{ |
|
_.memory.Free(annotatedUsers[i].id); |
|
_.memory.Free(annotatedUsers[i].annotation); |
|
} |
|
} |
|
|
|
private function AddGroup(BaseText groupName) |
|
{ |
|
if (_.users.IsGroupExisting(groupName)) |
|
{ |
|
callerConsole |
|
.Write(P("Group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.UseColorOnce(_.color.TextNegative) |
|
.Write(P(" already exists")) |
|
.WriteLine(P("!")); |
|
return; |
|
} |
|
if (_.users.AddGroup(groupName)) |
|
{ |
|
callerConsole |
|
.Write(P("Group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.UseColorOnce(_.color.TextPositive) |
|
.Write(P(" was added")) |
|
.WriteLine(P("!")); |
|
} |
|
else |
|
{ |
|
callerConsole |
|
.UseColorOnce(_.color.TextFailure) |
|
.Write(P("Cannot add")) |
|
.Write(P(" group with a name ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.WriteLine(P(" for unknown reason.")); |
|
} |
|
} |
|
|
|
private function RemoveGroup(BaseText groupName) |
|
{ |
|
if (!_.users.IsGroupExisting(groupName)) |
|
{ |
|
callerConsole |
|
.Write(P("Group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.UseColorOnce(_.color.TextNegative) |
|
.Write(P(" doesn't exists")) |
|
.WriteLine(P("!")); |
|
return; |
|
} |
|
if (_.users.RemoveGroup(groupName)) |
|
{ |
|
callerConsole |
|
.Write(P("Group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.UseColorOnce(_.color.TextPositive) |
|
.Write(P(" was removed")) |
|
.WriteLine(P("!")); |
|
} |
|
else |
|
{ |
|
callerConsole |
|
.UseColorOnce(_.color.TextFailure) |
|
.Write(P("Cannot remove")) |
|
.Write(P(" group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(groupName) |
|
.WriteLine(P(" for unknown reason.")); |
|
} |
|
} |
|
|
|
private function DisplayUserGroups() |
|
{ |
|
local int i; |
|
local array<Text> availableGroups; |
|
|
|
if (!ValidateUsersFeature()) { |
|
return; |
|
} |
|
availableGroups = _.users.GetAvailableGroups(); |
|
if (availableGroups.length <= 0) |
|
{ |
|
callerConsole.WriteLine(F("{$TextNegative No user groups}" |
|
@ "currently available.")); |
|
return; |
|
} |
|
callerConsole |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(P("Available user groups")) |
|
.Write(P(": ")); |
|
for (i = 0; i < availableGroups.length; i += 1) |
|
{ |
|
if (i > 0) { |
|
callerConsole.Write(P(", ")); |
|
} |
|
callerConsole.Write(availableGroups[i]); |
|
} |
|
callerConsole.Flush(); |
|
_.memory.FreeMany(availableGroups); |
|
} |
|
|
|
private function bool ValidateUsersFeature() |
|
{ |
|
if (class'Users_Feature'.static.IsEnabled()) { |
|
return true; |
|
} |
|
callerConsole |
|
.UseColorOnce(_.color.TextFailure) |
|
.WriteLine(P("`Users_Feature` is currently disabled.")); |
|
return false; |
|
} |
|
|
|
private function DisplayUserGroupsWithUsers() |
|
{ |
|
local int i; |
|
local array<Text> availableGroups; |
|
|
|
if (!ValidateUsersFeature()) { |
|
return; |
|
} |
|
availableGroups = _.users.GetAvailableGroups(); |
|
if (availableGroups.length <= 0) |
|
{ |
|
callerConsole.WriteLine(F("{$TextNegative No user groups}" |
|
@ "currently available.")); |
|
return; |
|
} |
|
for (i = 0; i < availableGroups.length; i += 1) |
|
{ |
|
callerConsole |
|
.Write(P("User group ")) |
|
.UseColorOnce(_.color.TextEmphasis) |
|
.Write(availableGroups[i]) |
|
.WriteLine(P(":")); |
|
DisplayUsersFor(availableGroups[i]); |
|
} |
|
callerConsole.Flush(); |
|
_.memory.FreeMany(availableGroups); |
|
} |
|
|
|
private function DisplayUsersFor(Text groupName) |
|
{ |
|
local int i; |
|
local Text nextID; |
|
local array<Users_Feature.AnnotatedUserID> annotatedUsers; |
|
|
|
annotatedUsers = _.users.GetAnnotatedGroupMembers(groupName); |
|
if (annotatedUsers.length <= 0) |
|
{ |
|
callerConsole.WriteBlock(P("No users")); |
|
return; |
|
} |
|
for (i = 0; i < annotatedUsers.length; i += 1) |
|
{ |
|
if (annotatedUsers[i].id == none) { |
|
continue; |
|
} |
|
nextID = annotatedUsers[i].id.GetUniqueID(); |
|
if (annotatedUsers[i].annotation != none) |
|
{ |
|
callerConsole |
|
.Write(nextID) |
|
.UseColorOnce(_.color.TextNeutral) |
|
.Write(P(" aka ")) |
|
.WriteBlock(annotatedUsers[i].annotation); |
|
} |
|
else { |
|
callerConsole.WriteBlock(nextID); |
|
} |
|
_.memory.Free(nextID); |
|
} |
|
for (i = 0; i < annotatedUsers.length; i += 1) |
|
{ |
|
_.memory.Free(annotatedUsers[i].id); |
|
_.memory.Free(annotatedUsers[i].annotation); |
|
} |
|
} |
|
|
|
defaultproperties |
|
{ |
|
} |