-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccessElement.cpp
161 lines (142 loc) · 5.15 KB
/
AccessElement.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <algorithm>
#include <iostream>
#include "AccessElement.h"
Spatch::Parsing::AccessElement::AccessElement()
{
}
Spatch::Parsing::AccessElement::~AccessElement()
{
}
void Spatch::Parsing::AccessElement::setId(std::string &id)
{
_id = id;
}
bool Spatch::Parsing::AccessElement::addProperty(std::string &key, std::string &value)
{
_prop[key] = value;
}
bool Spatch::Parsing::AccessElement::isValid() const
{
return true;
}
bool Spatch::Parsing::AccessElement::finish(Spatch::Configuration::Config &conf)
{
std::string server;
std::shared_ptr<Spatch::Configuration::Server> serv;
std::string credential;
Spatch::Configuration::Access::CredentialType type;
std::shared_ptr<Spatch::Configuration::Credential> cred;
std::string user;
std::shared_ptr<Spatch::Configuration::User> u;
std::cout << "Creating access " << _id << " with :" << std::endl;
for(auto const &prop : _prop) {
std::cout << "\t";
if (prop.first == "server")
server = prop.second;
else if (prop.first == "credential")
credential = prop.second;
else if (prop.first == "user")
user = prop.second;
else
std::cout << "(ignored) ";
std::cout << "[" << prop.first << "] = " << prop.second << std::endl;
}
if (user.empty() || server.empty() || credential.empty() || _id.empty())
{
std::cout << "[Error] Missing argument for access" << std::endl;
return false;
}
auto it = std::find_if(conf.getAccesses().begin(), conf.getAccesses().end(), [&] (std::shared_ptr<Spatch::Configuration::Access> a)
{
return (a->getId() == _id);
});
if(it != conf.getAccesses().end())
{
std::cout << "[Error] Duplicate access Id" << std::endl;
return false;
}
if (credential == "USER")
type = Spatch::Configuration::Access::CredentialType::UserCred;
else if (credential == "ASKED")
{
type = Spatch::Configuration::Access::CredentialType::Asked;
}
else if ((cred = getCredentialRef(conf, credential)) == nullptr)
{
std::cout << "[Error] Credential reference not found" << std::endl;
return false;
}
else
type = Spatch::Configuration::Access::CredentialType::Provided;
if ((serv = getServerRef(conf, server)) == nullptr)
{
std::cout << "[Error] Server reference not found" << std::endl;
return false;
}
if ((u = getUserRef(conf, user)) == nullptr)
{
std::cout << "[Error] User reference not found" << std::endl;
return false;
}
std::shared_ptr<Spatch::Configuration::Access> access;
if (type == Spatch::Configuration::Access::CredentialType::Provided)
access = std::make_shared<Spatch::Configuration::Access>(_id, u, cred, serv);
else
access = std::make_shared<Spatch::Configuration::Access>(_id, u, type, serv);
conf.addAccess(access);
std::cout << "[Success] Access " << _id << " created !" << std::endl;
return true;
}
std::shared_ptr<Spatch::Configuration::Server> Spatch::Parsing::AccessElement::getServerRef(Spatch::Configuration::Config &conf, std::string &ref)
{
size_t pos;
if ((pos = ref.find("$")) == std::string::npos)
return nullptr;
std::string id = ref.substr(pos + 1);
if (id.empty())
return nullptr;
auto it = std::find_if(conf.getServers().begin(), conf.getServers().end(), [&] (std::shared_ptr<Spatch::Configuration::Server> s)
{
return (s->getId() == id);
});
if(it == conf.getServers().end())
return nullptr;
return *it;
}
std::shared_ptr<Spatch::Configuration::Credential> Spatch::Parsing::AccessElement::getCredentialRef(Spatch::Configuration::Config &conf, std::string &ref)
{
size_t pos;
if ((pos = ref.find("$")) == std::string::npos)
return nullptr;
std::string id = ref.substr(pos + 1);
if (id.empty())
return nullptr;
auto it = std::find_if(conf.getCredentials().begin(), conf.getCredentials().end(), [&] (std::shared_ptr<Spatch::Configuration::Credential> c)
{
return (c->getId() == id);
});
if(it == conf.getCredentials().end())
return nullptr;
return *it;
}
std::shared_ptr<Spatch::Configuration::User> Spatch::Parsing::AccessElement::getUserRef(Spatch::Configuration::Config &conf, std::string &ref)
{
size_t pos;
if ((pos = ref.find("$")) == std::string::npos)
return nullptr;
std::string id = ref.substr(pos + 1);
if (id.empty())
return nullptr;
auto it = std::find_if(conf.getUsers().begin(), conf.getUsers().end(), [&] (std::shared_ptr<Spatch::Configuration::User> u)
{
return (u->getId() == id);
});
if(it == conf.getUsers().end())
return nullptr;
return *it;
}