Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsFsTreeConsultation.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Filename: WsFsTreeConsultation.cpp
4  *
5  * Description:
6  *
7  * Created: 03/21/2013 04:04:50 AM
8  * Revision: none
9  *
10  * Author: Benoit Daccache (BD), ben.daccache@gmail.com
11  * Company:
12  *
13  */
14 
15 #include "WsFsTreeConsultation.h"
16 
18 #include <Search/WsSearch.h>
19 #include <boost/lexical_cast.hpp>
20 
22  m_updater(updater)
23 {
25 }
26 
27 int WsFsTreeConsultation::getPermissions( const std::set<std::string>& groups, const std::string& p)
28 {
29  NodePtr n;
31  /* Get node */
32  n = ft->eatPath(p);
33  if (n.get() == 0) {
34  /* Path not found */
35  LOG(INFO) << "WsFsTreeConsultation::getPermissions() : No such file or directory :" << p;
36  return ErrorCode::NotFound;
37  }
38  if (groups.count(m_conf->get("global", "admin_group", "administrator")) > 0) {
39  /* user is admin , readwrite perms*/
41  }
42  /* Check if user is allowed */
43  if (n.get()->isAllowed(groups)) {
44  /* Editor can edit only nodes where he has access */
45  if (groups.count(m_conf->get("global", "editor_group", "editor")) > 0) {
46  /* user is editor. read write perms */
48  } else {
49  /* user is a normal user. read perms */
50  return GlobalConfig::Read;
51  }
52  } else {
54  if (props->get("global", "public_site", "false") == "true") {
55  /* Public site read access granted */
56  return GlobalConfig::Read;
57  }
58  }
59  /* no access */
60  return ErrorCode::NoAccess;
61 }
62 
63 WsNodeProperties* WsFsTreeConsultation::getProperties ( const std::set<std::string>& groups, const std::string& p)
64 {
65  /* Get the node */
67  NodePtr n;
68  n = ft->eatPath(p);
69  if (n.get() == 0) {
70  /* Path not found */
71  LOG(INFO) << "WsFsTreeConsultation::getProperties() : No such file or directory :" << p;
72  return 0;
73  }
74  /* Check if user has access to it */
75  if (!n.get()->isAllowed(groups)) {
76  return 0;
77  }
78  return n.get()->getProperties().get();
79 }
80 
81 std::string WsFsTreeConsultation::getProperty( const std::set<std::string>& groups, const std::string& section, const std::string& p, const std::string& prop)
82 {
83  WsNodeProperties* props = this->getProperties(groups, p);
84  if ( props == 0)
85  return "";
86  return props->get(section, prop, "");
87 }
88 
89 int WsFsTreeConsultation::getLock(const std::set<std::string> groups, const std::string& uid, const std::string& path)
90 {
91  boost::mutex::scoped_lock lock(m_lockEditMutex);
92  int ld = 0;
93  try {
94  ld = boost::lexical_cast<int>(m_conf->get("global", "lock_duration", "3600"));
95  } catch (boost::bad_lexical_cast&) {
96  LOG(ERROR) << "WsFsTreeConsultation::getLock() : Cannot cast lock_duration to int. Check conf value";
97  ld = 3600;
98  }
100  NodePtr n;
101  n = ft->eatPath(path);
102  if (n.get() == 0) {
103  /* Path not found */
104  LOG(INFO) << "WsFsTreeConsultation::getLock : No such file or directory :" << path;
105  return ErrorCode::NotFound;
106  }
107  // Check if lock already exist and if yes if it is owned by this user
108  boost::filesystem::path root = m_updater->getLastTree()->getRootPath();
109  boost::filesystem::path p;
110  if (n.get()->isDirectory()) return -1; //Cannot lock directory
111  else p = n.get()->getPath().parent_path();
112  std::string name = n.get()->getName() + ".lock";
113  n.get()->getProperties()->createPropertiesDirectories();
114  p = root / p / GlobalConfig::PathToNodeLock / name;
115  std::string id = "";
116  std::string ts = "";
117  Json::Reader reader;
118  Json::Value v;
119  //Conf file exist
120  if (boost::filesystem::exists(p)) {
121  std::ifstream lock(p.c_str(), std::ifstream::binary);
122  bool bOk = reader.parse(lock, v, false);
123  lock.close();
124  if ( !bOk )
125  boost::filesystem::remove(p);
126  else {
127  id = v["uid"].asString();
128  ts = v["timestamp"].asString();
129  // File is already locked by someone else
130  if ( id != uid)
131  if (boost::lexical_cast<long>(ts) > getTimeMs() - (ld * 1000) )
132  return ErrorCode::Locked;
133  else boost::filesystem::remove(p); //timeout
134  }
135  }
136  // write updated data to file
137  long millis = getTimeMs();
138  ts = boost::lexical_cast<string>(millis);
139  v["uid"] = uid;
140  v["timestamp"] = ts;
141  ofstream out;
142  out.open(p.string().c_str(), ios::out | ios::trunc | ios::binary);
143  if (out.is_open()) {
144  out << v.toStyledString();
145  out.close();
146  return ld;
147  } else return ErrorCode::Failure;
148 }
149 
150 int WsFsTreeConsultation::putLock(const std::set<std::string> groups, const std::string& uid, const std::string& path)
151 {
152  boost::mutex::scoped_lock lock(m_lockEditMutex);
153  int ld = 0;
154  try {
155  ld = boost::lexical_cast<int>(m_conf->get("global", "lock_duration", "3600"));
156  } catch (boost::bad_lexical_cast&) {
157  LOG(ERROR) << "WsFsTreeConsultation::getLock() : Cannot cast lock_duration to int. Check conf value";
158  ld = 3600;
159  }
161  NodePtr n;
162  n = ft->eatPath(path);
163  if (n.get() == 0) {
164  /* Path not found */
165  LOG(INFO) << "WsFsTreeConsultation::putLock : No such file or directory :" << path;
166  return ErrorCode::NotFound;
167  }
168  // Check if lock already exist and if yes if it is owned by this user
169  boost::filesystem::path root = m_updater->getLastTree()->getRootPath();
170  boost::filesystem::path p;
171  if (n.get()->isDirectory()) return -1; //Cannot unlock directory
172  else p = n.get()->getPath().parent_path();
173  std::string name = n.get()->getName() + ".lock";
174  p = root / p / GlobalConfig::PathToNodeLock / name;
175  LOG(DEBUG) << "WsFsTreeConsultation::putLock() : path is " << p.string();
176  std::string id = "";
177  std::string ts = "";
178  Json::Reader reader;
179  Json::Value v;
180  //Conf file exist
181  if (boost::filesystem::exists(p)) {
182  std::ifstream lock(p.c_str(), std::ifstream::binary);
183  bool bOk = reader.parse(lock, v, false);
184  lock.close();
185  if ( !bOk ) {
186  boost::filesystem::remove(p);
187  return ErrorCode::Success;
188  } else {
189  id = v["uid"].asString();
190  ts = v["timestamp"].asString();
191  // File is already locked by someone else
192  if ( id != uid) {
193  if (boost::lexical_cast<long>(ts) > getTimeMs() - (ld * 1000) )
194  return ErrorCode::Locked;
195  else {
196  boost::filesystem::remove(p); //timeout existing lock
197  return ErrorCode::Success;
198  }
199  }
200  boost::filesystem::remove(p); //Lock is hold by current user so we can delete it
201  return 1;
202  }
203  }
204  // No lock found
205  return 1;
206 }
207 
208 
209 int WsFsTreeConsultation::isLocked(const std::set<std::string> groups, const std::string& uid, const std::string& path, std::string& id)
210 {
211  boost::mutex::scoped_lock lock(m_lockEditMutex);
212  int ld = 0;
213  try {
214  ld = boost::lexical_cast<int>(m_conf->get("global", "lock_duration", "3600"));
215  } catch (boost::bad_lexical_cast&) {
216  LOG(ERROR) << "WsFsTreeConsultation::isLocked() : Cannot cast lock_duration to int. Check conf value";
217  ld = 3600;
218  }
220  NodePtr n;
221  n = ft->eatPath(path);
222  if (n.get() == 0) {
223  /* Path not found */
224  LOG(INFO) << "WsFsTreeConsultation:isLocked : No such file or directory :" << path;
225  return ErrorCode::NotFound;
226  }
227  // Check if lock already exist and if yes if it is owned by this user
228  boost::filesystem::path root = m_updater->getLastTree()->getRootPath();
229  boost::filesystem::path p;
230  if (n.get()->isDirectory()) return -1; //Cannot lock directory
231  else p = n.get()->getPath().parent_path();
232  std::string name = n.get()->getName() + ".lock";
233  n.get()->getProperties()->createPropertiesDirectories();
234  p = root / p / GlobalConfig::PathToNodeLock / name;
235  std::string lid = "";
236  std::string ts = "";
237  Json::Reader reader;
238  Json::Value v;
239  //Conf file exist
240  if (boost::filesystem::exists(p)) {
241  std::ifstream lock(p.c_str(), std::ifstream::binary);
242  bool bOk = reader.parse(lock, v, false);
243  lock.close();
244  if ( !bOk ) {
245  boost::filesystem::remove(p);
246  return 1;
247  } else {
248  lid = v["uid"].asString();
249  ts = v["timestamp"].asString();
250  // File is already locked by someone else
251  if ( lid != uid) {
252  if (boost::lexical_cast<long>(ts) > getTimeMs() - (ld * 1000) ) {
253  id = lid;
254  return ErrorCode::Locked;
255  } else {
256  boost::filesystem::remove(p); //timeout existing lock
257  return 1;
258  }
259  }
260  boost::filesystem::remove(p); //Lock is hold by current user so we can delete it
261  return 1;
262  }
263  }
264  // No lock found
265  return 1;
266 }
267 
269 {
270  timeval time;
271  gettimeofday(&time, 0);
272  return (time.tv_sec * 1000) + (time.tv_usec / 1000);
273 }
274 
275 WsAccessTree* WsFsTreeConsultation::getAccessTree( const std::set<std::string>& groups)
276 {
278  NodePtr n;
279  int depth;
280  try {
281  depth = boost::lexical_cast<int>(m_conf->get("global", "max_menu_depth", "0"));
282  } catch (boost::bad_lexical_cast&) {
283  LOG(ERROR) << "WsFsTreeConsultation::getAccessTree() : Cannot cast depth to int. Check conf value. Assuming depth is unlimited";
284  depth = 0;
285  }
286  n = ft->getRoot();
287  if (n.get() == 0)
288  return 0;
289  WsAccessTree* tree = new WsAccessTree(n, groups, depth, ft->getRootPath(), ft->getStamp());
290  if (tree->build() == ErrorCode::Failure) {
291  return 0;
292  }
293  return tree;
294 }
295 
296 std::vector<WsResultItem> WsFsTreeConsultation::getSearchResults( const std::set<std::string>& groups, const std::string& terms)
297 {
299  WsSearch s(ft, terms);
300  if (s.load() < 0)
301  return vector<WsResultItem>();
302  return s.getResults(groups);
303 }
304 
306 {
307  return m_conf->get("global", "root_path", ".");
308 }
309 
310 std::vector<std::string> WsFsTreeConsultation::getTemplatesList( const std::set<std::string>& groups, const std::string& path)
311 {
312 }
313 
315 {
316  return m_updater->getLastTree()->getStamp();
317 }
Global properties.
const std::string & getFsTreeStamp()
Get the stamp of the last WsFileSystemTree.
boost::shared_ptr< WsAbstractNode > NodePtr
std::vector< std::string > getTemplatesList(const std::set< std::string > &groups, const std::string &path)
TODO.
#define DEBUG
Definition: WsLogger.h:27
WsGlobalProperties * m_conf
Global properties class.
const int ReadWrite
WsFsTreeUpdater * m_updater
std::vector< WsResultItem > getSearchResults(const std::set< std::string > &groups, const std::string &terms)
Get the results for searching for "terms".
const int Failure
WsNodeProperties * getProperties(const std::set< std::string > &groups, const std::string &p)
get properties of a node corresponding to the path
Reprensents an access tree.
Definition: WsAccessTree.h:24
int putLock(const std::set< std::string > groups, const std::string &uid, const std::string &path)
unlocks the file by deleting the .config/locks/filename.lock file Only the owner of the lock can unlo...
const int Read
Properties of a WsNode.
handles the update of the FileSystemTree
virtual int build()
Parses the fileSystemTree and builds the Tree containing only the nodes where the user has access...
int getLock(const std::set< std::string > groups, const std::string &uid, const std::string &path)
tries to acquire the lock for the path. @ return ErrorCode::Locked if the lock cannot be aquired beca...
const int NotFound
FileSystemTreePtr getLastTree()
returns the last created tree
#define LOG
Definition: WsLogger.h:22
const std::string getRootPath()
#define INFO
Definition: WsLogger.h:32
const int NoAccess
boost::shared_ptr< WsFileSystemTree > FileSystemTreePtr
int load()
Loads all search results.
Definition: WsSearch.cpp:27
WsFsTreeConsultation(WsFsTreeUpdater *updater)
Constructor.
std::string get(const std::string &section, const std::string &id, const std::string &def)
const std::string PathToNodeLock
const std::string Value
Definition: WsRequestType.h:39
int isLocked(const std::set< std::string > groups, const std::string &uid, const std::string &path, std::string &id)
check is the path is already locked
const int Locked
static WsGlobalProperties * instance()
int getPermissions(const std::set< std::string > &groups, const std::string &p)
Get the permissions for a path for a set of groups.
WsAccessTree * getAccessTree(const std::set< std::string > &groups)
Get the access tree starting from rootPath.
vector< WsResultItem > getResults(const std::set< std::string > &groups)
Get list of documents readable by person member of all groups passed in parameter.
Definition: WsSearch.cpp:41
const int Success
#define ERROR
Definition: WsLogger.h:42
Instanciates the search.
Definition: WsSearch.h:34
std::string get(const std::string &section, const std::string &id, const std::string &def)
std::string getProperty(const std::set< std::string > &groups, const std::string &section, const std::string &p, const std::string &prop)
get a property for a node corresponding to the path