1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2010 - 2015, Göteborg Bit Factory.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #include <cmake.h>
28 #include <iostream>
29 #include <stdlib.h>
30 #include <ConfigFile.h>
31 #include <taskd.h>
32 #include <text.h>
33
34 ////////////////////////////////////////////////////////////////////////////////
35 // taskd add org <org>
36 // taskd add group <org> <group>
37 // taskd add user <org> <user>
command_add(Database & db,const std::vector<std::string> & args)38 void command_add (Database& db, const std::vector <std::string>& args)
39 {
40 bool verbose = db._config->getBoolean ("verbose");
41
42 // Verify that root exists.
43 std::string root = db._config->get ("root");
44 if (root == "")
45 throw std::string ("ERROR: The '--data' option is required.");
46
47 Directory root_dir (root);
48 if (!root_dir.exists ())
49 throw std::string ("ERROR: The '--data' path does not exist.");
50
51 if (args.size () < 2)
52 throw std::string ("ERROR: Subcommand not specified - expected 'org', 'group' or 'user'.");
53
54 // Create an organization.
55 // org <org>
56 if (closeEnough ("org", args[1], 3))
57 {
58 if (args.size () < 3)
59 throw std::string ("Usage: taskd add [options] org <org>");
60
61 for (unsigned int i = 2; i < args.size (); ++i)
62 {
63 if (taskd_is_org (root_dir, args[i]))
64 throw std::string ("ERROR: Organization '") + args[i] + "' already exists.";
65
66 if (db.add_org (args[i]))
67 {
68 if (verbose)
69 std::cout << "Created organization '" << args[i] << "'\n";
70 }
71 else
72 throw std::string ("ERROR: Failed to create organization '") + args[i] + "'.";
73 }
74 }
75
76 // Create a group.
77 // group <org> <group>
78 else if (closeEnough ("group", args[1], 3))
79 {
80 if (args.size () < 4)
81 throw std::string ("Usage: taskd add [options] group <org> <group>");
82
83 if (! taskd_is_org (root_dir, args[2]))
84 throw std::string ("ERROR: Organization '") + args[1] + "' does not exist.";
85
86 for (unsigned int i = 3; i < args.size (); ++i)
87 {
88 if (taskd_is_group (root_dir, args[2], args[i]))
89 throw std::string ("ERROR: Group '") + args[i] + "' already exists.";
90
91 if (db.add_group (args[2], args[i]))
92 {
93 if (verbose)
94 std::cout << "Created group '" << args[i] << "' for organization '" << args[2] << "'\n";
95 }
96 else
97 throw std::string ("ERROR: Failed to create group '") + args[i] + "'.";
98 }
99 }
100
101 // Create a user.
102 // user <org> <user>
103 else if (closeEnough ("user", args[1], 3))
104 {
105 if (args.size () < 4)
106 throw std::string ("Usage: taskd add [options] user <org> <user>");
107
108 if (! taskd_is_org (root_dir, args[2]))
109 throw std::string ("ERROR: Organization '") + args[1] + "' does not exist.";
110
111 for (unsigned int i = 3; i < args.size (); ++i)
112 {
113 if (taskd_is_user (root_dir, args[2], args[i]))
114 throw std::string ("ERROR: User '") + args[i] + "' already exists.";
115
116 if (db.add_user (args[2], args[i]))
117 {
118 if (verbose)
119 std::cout << "Created user '" << args[i] << "' for organization '" << args[2] << "'\n";
120 }
121 else
122 throw std::string ("ERROR: Failed to create user '") + args[i] + "'.";
123 }
124 }
125
126 else
127 throw std::string ("ERROR: Unrecognized argument '") + args[1] + "'";
128 }
129
130 ////////////////////////////////////////////////////////////////////////////////
131 // taskd remove org <org>
132 // taskd remove group <org> <group>
133 // taskd remove user <org> <user>
command_remove(Database & db,const std::vector<std::string> & args)134 void command_remove (Database& db, const std::vector <std::string>& args)
135 {
136 bool verbose = db._config->getBoolean ("verbose");
137
138 // Verify that root exists.
139 std::string root = db._config->get ("root");
140 if (root == "")
141 throw std::string ("ERROR: The '--data' option is required.");
142
143 Directory root_dir (root);
144 if (!root_dir.exists ())
145 throw std::string ("ERROR: The '--data' path does not exist.");
146
147 if (args.size () < 2)
148 throw std::string ("ERROR: Subcommand not specified - expected 'org', 'group' or 'user'.");
149
150 // Remove an organization.
151 // org <org>
152 if (closeEnough ("org", args[1], 3))
153 {
154 if (args.size () < 3)
155 throw std::string ("Usage: taskd remove [options] org <org>");
156
157 for (unsigned int i = 2; i < args.size (); ++i)
158 {
159 if (! taskd_is_org (root_dir, args[i]))
160 throw std::string ("ERROR: Organization '") + args[i] + "' does not exist.";
161
162 if (db.remove_org (args[i]))
163 {
164 if (verbose)
165 std::cout << "Removed organization '" << args[i] << "'\n";
166 }
167 else
168 throw std::string ("ERROR: Failed to remove organization '") + args[i] + "'.";
169 }
170 }
171
172 // Remove a group.
173 // group <org> <group>
174 else if (closeEnough ("group", args[1], 3))
175 {
176 if (args.size () < 4)
177 throw std::string ("Usage: taskd remove [options] group <org> <group>");
178
179 if (! taskd_is_org (root_dir, args[2]))
180 throw std::string ("ERROR: Organization '") + args[2] + "' does not exist.";
181
182 for (unsigned int i = 3; i < args.size (); ++i)
183 {
184 if (! taskd_is_group (root_dir, args[2], args[i]))
185 throw std::string ("ERROR: Group '") + args[i] + "' does not exist.";
186
187 if (db.remove_group (args[2], args[i]))
188 {
189 if (verbose)
190 std::cout << "Removed group '" << args[i] << "' from organization '" << args[2] << "'\n";
191 }
192 else
193 throw std::string ("ERROR: Failed to remove group '") + args[i] + "'.";
194 }
195 }
196
197 // Remove a user.
198 // user <org> <user>
199 else if (closeEnough ("user", args[1], 3))
200 {
201 if (args.size () < 4)
202 throw std::string ("Usage: taskd remove [options] user <org> <password>");
203
204 if (! taskd_is_org (root_dir, args[2]))
205 throw std::string ("ERROR: Organization '") + args[2] + "' does not exist.";
206
207 for (unsigned int i = 3; i < args.size (); ++i)
208 {
209 if (! taskd_is_user (root_dir, args[2], args[i]))
210 throw std::string ("ERROR: User '") + args[i] + "' does not exists.";
211
212 if (db.remove_user (args[2], args[i]))
213 {
214 if (verbose)
215 std::cout << "Removed user '" << args[i] << "' from organization '" << args[2] << "'\n";
216 }
217 else
218 throw std::string ("ERROR: Failed to remove user '") + args[i] + "'.";
219 }
220 }
221
222 else
223 throw std::string ("ERROR: Unrecognized argument '") + args[1] + "'";
224 }
225
226 ////////////////////////////////////////////////////////////////////////////////
227 // taskd suspend org <org>
228 // taskd suspend group <org> <group>
229 // taskd suspend user <org> <user>
command_suspend(Database & db,const std::vector<std::string> & args)230 void command_suspend (Database& db, const std::vector <std::string>& args)
231 {
232 bool verbose = db._config->getBoolean ("verbose");
233
234 // Verify that root exists.
235 std::string root = db._config->get ("root");
236 if (root == "")
237 throw std::string ("ERROR: The '--data' option is required.");
238
239 Directory root_dir (root);
240 if (!root_dir.exists ())
241 throw std::string ("ERROR: The '--data' path does not exist.");
242
243 if (args.size () < 2)
244 throw std::string ("ERROR: Subcommand not specified - expected 'org', 'group' or 'user'.");
245
246 // Suspend an organization.
247 // org <org>
248 if (closeEnough ("org", args[1], 3))
249 {
250 if (args.size () < 3)
251 throw std::string ("Usage: taskd suspend [options] org <org>");
252
253 for (unsigned int i = 2; i < args.size (); ++i)
254 {
255 if (! taskd_is_org (root_dir, args[i]))
256 throw std::string ("ERROR: Organization '") + args[i] + "' does not exist.";
257
258 if (db.suspend (root_dir._data + "/orgs/" + args[i]))
259 {
260 if (verbose)
261 std::cout << "Suspended organization '" << args[i] << "'\n";
262 }
263 else
264 throw std::string ("ERROR: Failed to suspend organization '") + args[i] + "'.";
265 }
266 }
267
268 // Suspend a group.
269 // group <org> <group>
270 else if (closeEnough ("group", args[1], 3))
271 {
272 if (args.size () < 4)
273 throw std::string ("Usage: taskd suspend [options] group <org> <group>");
274
275 if (! taskd_is_org (root_dir, args[2]))
276 throw std::string ("ERROR: Organization '") + args[2] + "' does not exist.";
277
278 for (unsigned int i = 3; i < args.size (); ++i)
279 {
280 if (! taskd_is_group (root_dir, args[2], args[i]))
281 throw std::string ("ERROR: Group '") + args[i] + "' does not exist.";
282
283 if (db.suspend (root_dir._data + "/orgs/" + args[2] + "/groups/" + args[i]))
284 {
285 if (verbose)
286 std::cout << "Suspended group '" << args[i] << "' in organization '" << args[2] << "'\n";
287 }
288 else
289 throw std::string ("ERROR: Failed to suspend group '") + args[i] + "'.";
290 }
291 }
292
293 // Suspend a user.
294 // user <org> <password>
295 else if (closeEnough ("user", args[1], 3))
296 {
297 if (args.size () < 4)
298 throw std::string ("Usage: taskd suspend [options] user <org> <password>");
299
300 if (! taskd_is_org (root_dir, args[2]))
301 throw std::string ("ERROR: Organization '") + args[2] + "' does not exist.";
302
303 for (unsigned int i = 3; i < args.size (); ++i)
304 {
305 if (! taskd_is_user (root_dir, args[2], args[i]))
306 throw std::string ("ERROR: User '") + args[i] + "' does not exists.";
307
308 if (db.suspend (root_dir._data + "/orgs/" + args[2] + "/users/" + args[i]))
309 {
310 if (verbose)
311 std::cout << "Suspended user '" << args[i] << "' in organization '" << args[2] << "'\n";
312 }
313 else
314 throw std::string ("ERROR: Failed to suspend user '") + args[i] + "'.";
315 }
316 }
317
318 else
319 throw std::string ("ERROR: Unrecognized argument '") + args[1] + "'";
320 }
321
322 ////////////////////////////////////////////////////////////////////////////////
323 // taskd resume org <org>
324 // taskd resume group <org> <group>
325 // taskd resume user <org> <user>
command_resume(Database & db,const std::vector<std::string> & args)326 void command_resume (Database& db, const std::vector <std::string>& args)
327 {
328 bool verbose = db._config->getBoolean ("verbose");
329
330 // Verify that root exists.
331 std::string root = db._config->get ("root");
332 if (root == "")
333 throw std::string ("ERROR: The '--data' option is required.");
334
335 Directory root_dir (root);
336 if (!root_dir.exists ())
337 throw std::string ("ERROR: The '--data' path does not exist.");
338
339 if (args.size () < 1)
340 throw std::string ("ERROR: Subcommand not specified - expected 'org', 'group' or 'user'.");
341
342 // Resume an organization.
343 // org <org>
344 if (closeEnough ("org", args[1], 3))
345 {
346 if (args.size () < 3)
347 throw std::string ("Usage: taskd resume [options] org <org>");
348
349 for (unsigned int i = 2; i < args.size (); ++i)
350 {
351 if (! taskd_is_org (root_dir, args[i]))
352 throw std::string ("ERROR: Organization '") + args[i] + "' does not exist.";
353
354 if (db.resume (root_dir._data + "/orgs/" + args[i]))
355 {
356 if (verbose)
357 std::cout << "Resumed organization '" << args[i] << "'\n";
358 }
359 else
360 throw std::string ("ERROR: Failed to resume organization '") + args[i] + "'.";
361 }
362 }
363
364 // Resume a group.
365 // group <org> <group>
366 else if (closeEnough ("group", args[1], 3))
367 {
368 if (args.size () < 4)
369 throw std::string ("Usage: taskd resume [options] group <org> <group>");
370
371 if (! taskd_is_org (root_dir, args[2]))
372 throw std::string ("ERROR: Organization '") + args[2] + "' does not exist.";
373
374 for (unsigned int i = 3; i < args.size (); ++i)
375 {
376 if (! taskd_is_group (root_dir, args[2], args[i]))
377 throw std::string ("ERROR: Group '") + args[i] + "' does not exist.";
378
379 if (db.resume (root_dir._data + "/orgs/" + args[2] + "/groups/" + args[i]))
380 {
381 if (verbose)
382 std::cout << "Resumed group '" << args[i] << "' in organization '" << args[2] << "'\n";
383 }
384 else
385 throw std::string ("ERROR: Failed to resume group '") + args[i] + "'.";
386 }
387 }
388
389 // Resume a user.
390 // user <org> <user>
391 else if (closeEnough ("user", args[1], 3))
392 {
393 if (args.size () < 4)
394 throw std::string ("Usage: taskd resume [options] user <org> <password>");
395
396 if (! taskd_is_org (root_dir, args[2]))
397 throw std::string ("ERROR: Organization '") + args[2] + "' does not exist.";
398
399 for (unsigned int i = 3; i < args.size (); ++i)
400 {
401 if (! taskd_is_user (root_dir, args[2], args[i]))
402 throw std::string ("ERROR: User '") + args[i] + "' does not exists.";
403
404 if (db.resume (root_dir._data + "/orgs/" + args[2] + "/users/" + args[i]))
405 {
406 if (verbose)
407 std::cout << "Resumed user '" << args[i] << "' in organization '" << args[2] << "'\n";
408 }
409 else
410 throw std::string ("ERROR: Failed to resume user '") + args[i] + "'.";
411 }
412 }
413
414 else
415 throw std::string ("ERROR: Unrecognized argument '") + args[1] + "'";
416 }
417
418 ////////////////////////////////////////////////////////////////////////////////
419