utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.git] / src / auth.h
1 /*
2 Minetest-c55
3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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 2 of the License, or
8 (at your option) any later version.
9
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.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef AUTH_HEADER
21 #define AUTH_HEADER
22
23 #include <string>
24 #include <jthread.h>
25 #include <jmutex.h>
26 #include "common_irrlicht.h"
27 #include "exceptions.h"
28
29 // Player privileges. These form a bitmask stored in the privs field
30 // of the player, and define things they're allowed to do. See also
31 // the static methods Player::privsToString and stringToPrivs that
32 // convert these to human-readable form.
33 const u64 PRIV_BUILD = 1;            // Can build - i.e. modify the world
34 const u64 PRIV_TELEPORT = 2;         // Can teleport
35 const u64 PRIV_SETTIME = 4;          // Can set the time
36 const u64 PRIV_PRIVS = 8;            // Can grant and revoke privileges
37 const u64 PRIV_SERVER = 16;          // Can manage the server (e.g. shutodwn
38                                      // ,settings)
39 const u64 PRIV_SHOUT = 32;           // Can broadcast chat messages to all
40                                      // players
41 const u64 PRIV_BAN = 64;             // Can ban players
42
43 // Default privileges - these can be overriden for new players using the
44 // config option "default_privs" - however, this value still applies for
45 // players that existed before the privileges system was added.
46 const u64 PRIV_DEFAULT = PRIV_BUILD|PRIV_SHOUT;
47 const u64 PRIV_ALL = 0x7FFFFFFFFFFFFFFFULL;
48 const u64 PRIV_INVALID = 0x8000000000000000ULL;
49
50 // Convert a privileges value into a human-readable string,
51 // with each component separated by a comma.
52 std::string privsToString(u64 privs);
53
54 // Converts a comma-seperated list of privilege values into a
55 // privileges value. The reverse of privsToString(). Returns
56 // PRIV_INVALID if there is anything wrong with the input.
57 u64 stringToPrivs(std::string str);
58
59 struct AuthData
60 {
61         std::string pwd;
62         u64 privs;
63
64         AuthData():
65                 privs(PRIV_DEFAULT)
66         {
67         }
68 };
69
70 class AuthNotFoundException : public BaseException
71 {
72 public:
73         AuthNotFoundException(const char *s):
74                 BaseException(s)
75         {}
76 };
77
78 class AuthManager
79 {
80 public:
81         AuthManager(const std::string &authfilepath);
82         ~AuthManager();
83         void load();
84         void save();
85         bool exists(const std::string &username);
86         void set(const std::string &username, AuthData ad);
87         void add(const std::string &username);
88         std::string getPassword(const std::string &username);
89         void setPassword(const std::string &username,
90                         const std::string &password);
91         u64 getPrivs(const std::string &username);
92         void setPrivs(const std::string &username, u64 privs);
93         bool isModified();
94 private:
95         JMutex m_mutex;
96         std::string m_authfilepath;
97         core::map<std::string, AuthData> m_authdata;
98         bool m_modified;
99 };
100
101 #endif
102