| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_DB_ROLE_HH |
|---|
| 21 | #define GUARD_DB_ROLE_HH |
|---|
| 22 | |
|---|
| 23 | #include <boost/noncopyable.hpp> |
|---|
| 24 | #include <boost/shared_ptr.hpp> |
|---|
| 25 | |
|---|
| 26 | #include <set> |
|---|
| 27 | #include <string> |
|---|
| 28 | |
|---|
| 29 | namespace db |
|---|
| 30 | { |
|---|
| 31 | class user; |
|---|
| 32 | class role : private boost::noncopyable |
|---|
| 33 | { |
|---|
| 34 | std::string name; |
|---|
| 35 | std::string description; |
|---|
| 36 | std::set<boost::shared_ptr<user> > users; |
|---|
| 37 | |
|---|
| 38 | friend class db; |
|---|
| 39 | friend class user; |
|---|
| 40 | |
|---|
| 41 | static const std::string subscriber_prefix; |
|---|
| 42 | static const std::string moderator_prefix; |
|---|
| 43 | public: |
|---|
| 44 | typedef boost::shared_ptr<role> ptr; |
|---|
| 45 | typedef boost::shared_ptr<const role> const_ptr; |
|---|
| 46 | |
|---|
| 47 | role(std::string name, std::string descr) |
|---|
| 48 | : name(name), description(descr) |
|---|
| 49 | {} |
|---|
| 50 | |
|---|
| 51 | std::string get_name() const { return name; } |
|---|
| 52 | std::string get_description() const { return name; } |
|---|
| 53 | |
|---|
| 54 | bool is_moderator() { |
|---|
| 55 | return name.substr(0, moderator_prefix.length()) |
|---|
| 56 | == |
|---|
| 57 | moderator_prefix; |
|---|
| 58 | } |
|---|
| 59 | bool is_moderator_for(std::string grn); |
|---|
| 60 | |
|---|
| 61 | const std::set<boost::shared_ptr<user> > &get_users() const { |
|---|
| 62 | return users; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | static ptr anon() { |
|---|
| 66 | static ptr rv; |
|---|
| 67 | if (!rv) rv.reset(new role("anonymous", |
|---|
| 68 | "Anonymous user")); |
|---|
| 69 | return rv; |
|---|
| 70 | } |
|---|
| 71 | static ptr authn() { |
|---|
| 72 | static ptr rv; |
|---|
| 73 | if (!rv) rv.reset(new role("authenticated", |
|---|
| 74 | "Authenticated user")); |
|---|
| 75 | return rv; |
|---|
| 76 | } |
|---|
| 77 | static ptr poster() { |
|---|
| 78 | static ptr rv; |
|---|
| 79 | if (!rv) rv.reset(new role("poster", |
|---|
| 80 | "Basic poster authorization") |
|---|
| 81 | ); |
|---|
| 82 | return rv; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | bool is_subscriber_role() const |
|---|
| 86 | { return is_subscriber_role(name); } |
|---|
| 87 | |
|---|
| 88 | std::string whose_subscriber_role() const { |
|---|
| 89 | return name.substr(subscriber_prefix.length()); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | static bool is_subscriber_role(std::string str) { |
|---|
| 93 | return str.substr(0, subscriber_prefix.length()) |
|---|
| 94 | == |
|---|
| 95 | subscriber_prefix; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | static std::string get_subscriber_role(std::string group) { |
|---|
| 99 | return subscriber_prefix + group; |
|---|
| 100 | } |
|---|
| 101 | }; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | #endif |
|---|