This repository was archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessStrategy.cpp
More file actions
135 lines (115 loc) · 3.79 KB
/
AccessStrategy.cpp
File metadata and controls
135 lines (115 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2019 Riccardo Zaccone, Ksenia Del Conte Akimova, Alice Morano, Martina Bellissimo
*
* This file is part of Symposium.
* Symposium 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, either version 3 of the License, or
* (at your option) any later version.
*
* Symposium 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 Symposium. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* File: AccessStrategy.cpp
* Project: Symposium
* Authors:
* Riccardo Zaccone <riccardo.zaccone at studenti.polito.it>
* Ksenia Del Conte Akimova <s256669 at studenti.polito.it>
* Alice Morano <s259158 at studenti.polito.it>
* Martina Bellissimo <s257307 at studenti.polito.it>
*
* Created on 05 Luglio 2019, 16.22
*/
#include "AccessStrategy.h"
using namespace Symposium;
bool RMOAccess::validateAction(const std::string &targetUser, privilege requested) const {
if(permission.empty() && requested==privilege::owner)
return true;
if(requested==privilege::none)
return false;
std::unordered_map<std::string,privilege >::const_iterator got = permission.find(targetUser);
privilege attuale;
if ( got == permission.end() )
{
attuale=privilege::none;
return attuale==requested;
}
attuale=got->second;
return attuale >= requested;
}
privilege RMOAccess::setPrivilege(const std::string &targetUser, privilege toGrant) {
if(permission.empty() && toGrant==privilege::owner)
{
permission.insert (std::make_pair(targetUser, toGrant));
return privilege::none;
}
auto got = permission.find(targetUser);
if ( got == permission.end())
{
if(toGrant==privilege::none)
return privilege::none;
permission.insert (std::make_pair(targetUser, toGrant));
return privilege::none;
}
privilege vecchio=got->second;
if(toGrant==privilege::none)
permission.erase (targetUser);
else
got->second=toGrant;
return vecchio;
}
privilege RMOAccess::getPrivilege(const std::string &targetUser) const
{
std::unordered_map<std::string,privilege >::const_iterator got = permission.find(targetUser);
if ( got == permission.end() )
return privilege::none;
return got->second;
}
bool RMOAccess::moreOwner(std::string username) const
{
int i=0;
bool own=false;
for(auto& tuple : permission)
{
if(tuple.second==privilege::owner)
{
i++;
if(tuple.first==username)
own=true;
}
}
return !((i==1) && own);
}
std::unordered_map<std::string, privilege> RMOAccess::getPermission() const
{
return permission;
}
bool RMOAccess::operator==(const RMOAccess &rhs) const {
return permission == rhs.permission;
}
bool RMOAccess::operator!=(const RMOAccess &rhs) const {
return !(rhs == *this);
}
bool TrivialAccess::validateAction(const std::string &, privilege) const {
return true;
}
privilege TrivialAccess::setPrivilege(const std::string &, privilege) {
return privilege::none;
}
privilege TrivialAccess::getPrivilege(const std::string &) const {
return privilege::none;
}
bool TrivialAccess::moreOwner(std::string) const{
return false;
}
std::unordered_map<std::string, privilege> TrivialAccess::getPermission() const
{
return std::unordered_map<std::string, privilege>();
}
BOOST_CLASS_EXPORT(Symposium::RMOAccess)
BOOST_CLASS_EXPORT(Symposium::TrivialAccess)