Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsNodeProperties.cpp
Go to the documentation of this file.
1 
14 #include "WsNodeProperties.h"
15 #include <Include/WsGlobalConfig.h>
16 #include "WsGlobalProperties.h"
17 #include <iostream>
18 
19 #include <set>
20 using namespace std;
21 
22 using namespace Json;
23 
24 using namespace boost::filesystem;
25 
26 
27 WsNodeProperties::WsNodeProperties(boost::filesystem::path nodePath, Type t):
29  m_nodePath(nodePath),
30  m_type(t)
31 {
32 }
33 
34 WsNodeProperties::WsNodeProperties(string jsonInput)
35 {
36  Reader r;
37  if (r.parse(jsonInput, m_root, false))
38  m_parsed = true;
39 }
40 
42 {
43 }
44 
45 string WsNodeProperties::get(const string& section, const string& id, const string& def)
46 {
47  {
48  boost::mutex::scoped_lock lock(m_mutex);
49  if (!m_parsed) {
50  string p = getPath();
51  if (parse(p) == ErrorCode::Failure)
52  return string();
53  }
54  Value val = m_root[section][id];
55  if (val == Value::null || val.asString() == "null") {
56  return def;
57  } else return val.asString();
58  }
59 }
60 
61 
62 std::set<string> WsNodeProperties::getGroups()
63 {
64  std::set<std::string> grp;
65  {
66  boost::mutex::scoped_lock lock(m_mutex);
67  if (!m_parsed) {
68  string p = getPath();
69  if (parse(p) == ErrorCode::Failure)
70  return grp;
71  }
72  const Value groups = m_root["global"]["groups"];
73  if (groups != Value::null) {
74  for ( int i = 0; i < groups.size(); ++i) {
75  grp.insert(groups[i].asString());
76  }
77  }
78  return grp;
79  }
80 }
81 
82 bool WsNodeProperties::isAllowed(std::set<string> gids)
83 {
84  std::set<string> nodeGroups = getGroups();
85  {
86  boost::mutex::scoped_lock lock(m_mutex);
87  if (!m_parsed) {
88  string p = getPath();
89  if (parse(p) == ErrorCode::Failure)
90  return false;
91  }
93  string admgrp = props->get("global", "admin_group", "");
94  /* Admin has access to everything */
95  if (gids.count(admgrp) > 0 ) {
96  LOG(DEBUG) << "WsNodeProperties::isAllowed() : User is Admin. Access allowed for " << getPath();
97  return true;
98  }
99  std::set<string>::iterator itGids;
100  /* Iterate over the list of users allowed and check */
101  /* if there is a similar entry in the gids list */
102  for (itGids = gids.begin(); itGids != gids.end(); ++itGids) {
103  if (nodeGroups.count(*itGids) > 0) {
104  /* We found a similar entry, we need to check publish date */
105  /* Editor has access to node even if not published yet */
106  string edtgrp = props->get("global", "editor_group", "");
107  if (gids.count(edtgrp) > 0 ) {
108  LOG(DEBUG) << "WsNodeProperties::isAllowed() User is Editor. Access allowed for " << getPath();
109  return true;
110  }
111  LOG(DEBUG) << "WsNodeProperties::isAllowed() : User not editor";
112  try {
113  time_t pnode = boost::lexical_cast<time_t>(this->get("global", "publish_date", "0"));
114  time_t pnow = time(NULL);
115  if (pnode <= pnow) {
116  return true;
117  } else {
118  LOG(DEBUG) << "WsNodeProperties::isAllowed() : File is not published yet. Access Denied for " << getPath();
119  return false;
120  }
121  } catch (boost::bad_lexical_cast&) {
122  LOG(ERROR) << "WsNodeProperties::isAllowed() : Cannot parse publish date for " << this->getPath();
123  return false;
124  }
125  }
126  }
127  /* No similar entries found. Group has no access */
128  LOG(DEBUG) << "WsNodeProperties :: User has no access ";
129  return false;
130  }
131 }
132 
134 {
135  {
136  boost::mutex::scoped_lock lock(m_mutex);
137  if (!m_parsed) {
138  string p = getPath();
139  if (parse(p) == ErrorCode::Failure)
140  return Value();
141  }
142  return m_root;
143  }
144 }
145 
147 {
148  m_root = root;
149  m_parsed = true;
150 }
151 
152 void WsNodeProperties::setRoot(const std::string& jsonInput)
153 {
154  {
155  boost::mutex::scoped_lock lock(m_mutex);
156  Reader r;
157  if (r.parse(jsonInput, m_root, false))
158  m_parsed = true;
159  else
160  m_parsed = false;
161  }
162 }
163 
164 void WsNodeProperties::set(const string& section, const string& key, const string& value)
165 {
166  if (key.size() == 0 || value.size() == 0)
167  return;
168  {
169  boost::mutex::scoped_lock lock(m_mutex);
170  if (!m_parsed) {
171  string p = getPath();
172  if (parse(p) == ErrorCode::Failure)
173  return;
174  }
175  m_root[section][key] = value;
176  }
177 }
178 
179 void WsNodeProperties::setGroups(std::set<string> grps)
180 {
181  {
182  boost::mutex::scoped_lock lock(m_mutex);
183  if (!m_parsed) {
184  string p = getPath();
185  if (parse(p) == ErrorCode::Failure)
186  return;
187  }
188  m_root["global"]["groups"].clear();
189  std::set<string>::iterator it;
190  int i;
191  for (it = grps.begin(), i = 0; it != grps.end(); ++it, ++i) {
192  m_root["global"]["groups"][i] = *it;
193  }
194  }
195 }
196 
198 {
199  {
200  boost::mutex::scoped_lock try_lock(m_mutex);
201  try {
202  path p = m_nodePath;
203  for (int i = 0; i < GlobalConfig::NbItems; ++i) {
204  if ( !exists(p / GlobalConfig::SubFolders[i]) ) {
205  create_directory(p / GlobalConfig::SubFolders[i]);
206  LOG(DEBUG) << "WsNodeProperties::createPropertiesDirectories() : Creating Config dir " << (p / GlobalConfig::SubFolders[i]).string();
207  }
208  }
209  return ErrorCode::Success;
210  } catch (exception& e) {
211  LOG(ERROR) << "WsNodeProperties::createPropertiesDirectories() : " << e.what();
212  }
213  }
214  return ErrorCode::Success;
215 }
216 
218 {
219  boost::mutex::scoped_lock lock(m_mutex);
220  ofstream conf;
221  string p = getPath();
222  conf.open(p.c_str(), ios::out | ios::trunc | ios::binary);
223  if (conf.is_open()) {
224  conf << m_root.toStyledString();
225  conf.close();
226  return ErrorCode::Success;
227  }
228  return ErrorCode::Failure;
229 }
230 
232 {
233  string p;
234  if (m_type == Dir) {
235  p += m_nodePath.string();
237  } else {
238  p += m_nodePath.parent_path().string();
240  }
241  return p;
242 }
243 
Global properties.
std::string getPath()
Get path of the node which the configuration belongs to.
#define DEBUG
Definition: WsLogger.h:27
const std::string SubFolders[NbItems]
Global properties class.
boost::filesystem::path m_nodePath
int createPropertiesDirectories()
Creates the directories for the properties.
const int Failure
std::set< std::string > getGroups()
Returns the groups that have access to the node If no groups are found in the file, an empty set is returned.
int save()
save the properties to the disk The path of the file is set in m_nodePath and is set when building th...
int parse(const std::string &path)
parse the Json config file
const std::string PathToNodeProperties
void setRoot(Json::Value root)
set the root of the Json tree
#define LOG
Definition: WsLogger.h:22
const int NbItems
Json::Value getRoot()
Return the root of the Json tree.
Abstract Properties class.
std::string get(const std::string &section, const std::string &id, const std::string &def)
const std::string Value
Definition: WsRequestType.h:39
WsNodeProperties(boost::filesystem::path nodePath, Type t)
Constructor.
static WsGlobalProperties * instance()
const std::string PathToDirProperties
void set(const std::string &section, const std::string &key, const std::string &value)
Changes or sets the propety with key 'key' to value 'value' If the property already exist...
const int Success
bool isAllowed(std::set< std::string > gids)
Compare the gids with the node groups. If the user has access, true is returned, otherwise false is r...
const std::string ConfExt
void setGroups(std::set< std::string > grps)
Sets the groups of the node to grps. All groups will be overriden. This method does not however...
#define ERROR
Definition: WsLogger.h:42
std::string get(const std::string &section, const std::string &id, const std::string &def)
boost::mutex m_mutex