1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "EditorPermission.h"
22 #include "EditorOLC.h"
25 #include "StringUtilities.h"
26 #include "TableImpls.h"
28 #include "AccountManager.h"
29 #include "Permission.h"
30 #include "PermissionManager.h"
31 #include "GrantGroup.h"
32 #include "GrantGroupManager.h"
35 using mud::Permission
;
37 typedef EditorPermission E
;
38 typedef CommandInfoObject
<E
> O
;
39 typedef CommandBinding
<E
> B
;
41 // name function need: object lock
42 static O
editAccount( "Account", &E::editAccount
, true, true);
43 static O
editGrantGroup("GrantGroup",&E::editGrantGroup
,true, true);
44 static O
editGrant( "Grant", &E::editGrant
, true, true);
45 static O
editLogging( "Logging", &E::editLogging
, true, true);
46 static O
showPermission("Show", &E::showPermission
, true, false);
47 static O
savePermission("Save", &E::savePermission
, true, true);
49 static const B commands
[] = {
50 B("account", editAccount
),
51 B("grant", editGrant
),
52 B("grantgroup", editGrantGroup
),
53 B("logging", editLogging
),
54 B("save", savePermission
),
55 B("show", showPermission
),
58 EditorPermission::EditorPermission(UBSocket
* sock
) :
60 m_commands(commands
, array_size(commands
)),
63 listCommands(Global::Get()->EmptyString
);
66 EditorPermission::~EditorPermission(void)
71 std::string
EditorPermission::lookup(const std::string
& action
)
73 std::string name
= OLCEditor::lookup(action
);
77 const PermissionCommand
* act
= (PermissionCommand
*)m_commands
.getObject(action
);
79 return act
->getName();
81 return Global::Get()->EmptyString
;
84 void EditorPermission::dispatch(const std::string
& action
, const std::string
& argument
)
86 const PermissionCommand
* act
= (PermissionCommand
*)m_commands
.getObject(action
);
90 OLCEditor::dispatch(action
, argument
);
96 m_sock
->Send("You need to be editing a permission first.\n");
97 m_sock
->Send("(Use the 'edit' command.)\n");
104 m_permission
->Lock();
105 } catch(SavableLocked
& e
) {
106 m_sock
->Send("The permission you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
107 m_sock
->Send("Please try again later.\n");
112 act
->Run(this, argument
);
116 SavablePtr
EditorPermission::getEditing()
121 TableImplPtr
EditorPermission::getTable()
123 return db::TableImpls::Get()->PERMISSIONS
;
126 KeysPtr
EditorPermission::addNew()
128 throw std::runtime_error("EditorPermission::addNew(), Not yet implemented.");
131 std::vector
<std::string
> EditorPermission::getList()
133 return mud::Managers::Get()->Permission
->List();
136 void EditorPermission::setEditing(KeysPtr keys
)
140 m_permission
.reset();
144 mud::PermissionPtr permission
= mud::Managers::Get()->Permission
->GetByKeys(keys
);
145 m_permission
= permission
;
149 std::vector
<std::string
> EditorPermission::getCommands()
151 return m_commands
.getCommandsVector();
154 void EditorPermission::editAccount(const std::string
& argument
)
156 if(argument
.size() == 0)
158 m_sock
->Send("Please specify an account!\n");
164 long id
= mud::Managers::Get()->Account
->lookupByName(argument
);
165 m_sock
->Sendf("Preparing to edit permission with account '%d'.\n", id
);
168 catch(RowNotFoundException
& e
)
170 m_sock
->Sendf("'%s' is not a valid account!\n", argument
.c_str());
171 m_sock
->Send(String::Get()->box(mud::Managers::Get()->Account
->List(), "Accounts"));
175 void EditorPermission::editGrantGroup(const std::string
& argument
)
177 if(argument
.size() == 0)
179 m_sock
->Send("Please specify a grantgroup!\n");
185 long id
= mud::Managers::Get()->GrantGroup
->lookupByName(argument
);
186 m_sock
->Sendf("Preparing to edit permission with grangroup '%d'.\n", id
);
189 catch(RowNotFoundException
& e
)
191 m_sock
->Sendf("'%s' is not a valid grantgroup!\n", argument
.c_str());
192 m_sock
->Send(String::Get()->box(mud::Managers::Get()->GrantGroup
->List(), "GrantGroups"));
196 void EditorPermission::editGrant(const std::string
& argument
)
198 if(!argument
.compare("Enable"))
200 m_permission
->setGrant(true);
204 if(!argument
.compare("Disable"))
206 m_permission
->setGrant(false);
210 m_sock
->Send("Please specify a grant level!\n");
211 m_sock
->Send("Choose between 'Enable' and 'Disable'.\n");
215 void EditorPermission::editLogging(const std::string
& argument
)
217 if(!argument
.compare("Enable"))
219 m_permission
->setLog(true);
223 if(!argument
.compare("Disable"))
225 m_permission
->setLog(false);
229 m_sock
->Send("Please specify a log level!\n");
230 m_sock
->Send("Choose bewteen 'Enable' and 'Disable'.\n");
234 void EditorPermission::savePermission(const std::string
& argument
)
236 m_sock
->Send("Saving...\n");
237 m_permission
->Save();
238 m_sock
->Send("Saved.\n");
242 void EditorPermission::showPermission(const std::string
& argument
)
244 m_sock
->Send(m_permission
->toString());