DBBase.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. // -*- Mode: c++ -*-
  18. #ifndef __DBBASE_H
  19. #define __DBBASE_H
  20. #include <kopano/zcdefs.h>
  21. #include "plugin.h"
  22. #include <memory>
  23. #include <mutex>
  24. #include <string>
  25. #include "ECDatabase.h"
  26. #include <kopano/ECDefs.h>
  27. namespace KC {
  28. /**
  29. * @defgroup userplugin_dbbase Database common for user plugins
  30. * @ingroup userplugin
  31. * @{
  32. */
  33. // Table names
  34. #define DB_OBJECT_TABLE "object"
  35. #define DB_OBJECTPROPERTY_TABLE "objectproperty"
  36. #define DB_OBJECTMVPROPERTY_TABLE "objectmvproperty"
  37. #define DB_OBJECT_RELATION_TABLE "objectrelation"
  38. // Object properties
  39. #define OP_MODTIME "modtime"
  40. #define OP_LOGINNAME "loginname"
  41. #define OP_PASSWORD "password"
  42. #define OP_ISADMIN "isadmin"
  43. #define OB_AB_HIDDEN "ishidden"
  44. #define OP_FULLNAME "fullname"
  45. #define OP_FIRSTNAME "firstname"
  46. #define OP_EMAILADDRESS "emailaddress"
  47. #define OP_HARDQUOTA "hardquota"
  48. #define OP_SOFTQUOTA "softquota"
  49. #define OP_WARNQUOTA "warnquota"
  50. #define OP_USEDEFAULTQUOTA "usedefaultquota"
  51. #define OP_UD_HARDQUOTA "userhardquota"
  52. #define OP_UD_SOFTQUOTA "usersoftquota"
  53. #define OP_UD_WARNQUOTA "userwarnquota"
  54. #define OP_UD_USEDEFAULTQUOTA "userusedefaultquota"
  55. #define OP_GROUPNAME "groupname"
  56. #define OP_COMPANYID "companyid"
  57. #define OP_COMPANYNAME "companyname"
  58. #define OP_COMPANYADMIN "companyadmin"
  59. /**
  60. * User managment helper class for database access
  61. *
  62. * This class implements most of the Database Access functions
  63. * which can be shared between the DB and Unix plugin.
  64. */
  65. class DBPlugin : public UserPlugin {
  66. public:
  67. /**
  68. * @param[in] pluginlock
  69. * The plugin mutex
  70. * @param[in] shareddata
  71. * The singleton shared plugin data.
  72. * @throw std::exception
  73. */
  74. DBPlugin(std::mutex &, ECPluginSharedData *shareddata);
  75. /**
  76. * Initialize plugin
  77. *
  78. * @throw runtime_error when the database could not be initialized
  79. */
  80. virtual void InitPlugin();
  81. /**
  82. * Request a list of objects for a particular company and specified objectclass.
  83. *
  84. * This will only create a query and will call DBPlugin::CreateSignatureList()
  85. *
  86. * @param[in] company
  87. * The company beneath which the objects should be listed.
  88. * This objectid can be empty.
  89. * @param[in] objclass
  90. * The objectclass of the objects which should be returned.
  91. * The objectclass can be partially unknown (OBJECTCLASS_UNKNOWN, MAILUSER_UNKNOWN, ...)
  92. * @return The list of object signatures of all objects which were found
  93. * @throw std::exception
  94. */
  95. virtual std::unique_ptr<signatures_t> getAllObjects(const objectid_t &company, objectclass_t objclass);
  96. /**
  97. * Obtain the object details for the given object
  98. *
  99. * This calls DBPlugin::getObjectDetails(const list<objectid_t> &objectids)
  100. *
  101. * @param[in] objectid
  102. * The objectid for which is details are requested
  103. * @return The objectdetails for the given objectid
  104. * @throw objectnotfound when the object was not found
  105. */
  106. virtual std::unique_ptr<objectdetails_t> getObjectDetails(const objectid_t &objectid);
  107. /**
  108. * Obtain the object details for the given objects
  109. *
  110. * It is possible that the returned map contains less elements then objectids.size()
  111. * when not all objects where found.
  112. *
  113. * @param[in] objectids
  114. * The list of object signatures for which the details are requested
  115. * @return A map of objectid with the matching objectdetails
  116. * @throw runtime_error when SQL problems occur.
  117. */
  118. virtual std::unique_ptr<std::map<objectid_t, objectdetails_t> > getObjectDetails(const std::list<objectid_t> &objectids);
  119. /**
  120. * Get all children for a parent for a given relation type.
  121. * For example all users in a group
  122. *
  123. * This will only create a query and will call DBPlugin::CreateSignatureList()
  124. *
  125. * @param[in] relation
  126. * The relation type which connects the child and parent object
  127. * @param[in] parentobject
  128. * The parent object for which the children are requested
  129. * @return A list of object signatures of the children of the parent.
  130. * @throw std::exception
  131. */
  132. virtual std::unique_ptr<signatures_t> getSubObjectsForObject(userobject_relation_t relation, const objectid_t &parentobject);
  133. /**
  134. * Request all parents for a childobject for a given relation type.
  135. * For example all groups for a user
  136. *
  137. * This will only create a query and will call DBPlugin::CreateSignatureList()
  138. *
  139. * @param[in] relation
  140. * The relation type which connects the child and parent object
  141. * @param[in] childobject
  142. * The childobject for which the parents are requested
  143. * @return A list of object signatures of the parents of the child.
  144. * @throw std::exception
  145. */
  146. virtual std::unique_ptr<signatures_t> getParentObjectsForObject(userobject_relation_t relation, const objectid_t &childobject);
  147. /**
  148. * Update an object with new details
  149. *
  150. * @param[in] id
  151. * The object id of the object which should be updated.
  152. * @param[in] details
  153. * The objectdetails which should be written to the object.
  154. * @param[in] lpRemove
  155. * List of configuration names which should be removed from the object
  156. * @throw runtime_error when SQL problems occur.
  157. */
  158. virtual void changeObject(const objectid_t &id, const objectdetails_t &details, const std::list<std::string> *lpRemove);
  159. /**
  160. * Create object in plugin
  161. *
  162. * @param[in] details
  163. * The object details of the new object.
  164. * @return The objectsignature of the created object.
  165. * @throw runtime_error When SQL problems occur.
  166. * @throw collison_error When the object already exists.
  167. */
  168. virtual objectsignature_t createObject(const objectdetails_t &details);
  169. /**
  170. * Delete object from plugin
  171. *
  172. * @param[in] id
  173. * The objectid which should be deleted
  174. * @throw runtime_error When SQL problems occur.
  175. * @throw objectnotfound When the object did not exist.
  176. */
  177. virtual void deleteObject(const objectid_t &id);
  178. /**
  179. * Add relation between child and parent. This can be used
  180. * for example to add a user to a group or add
  181. * permission relations on companies.
  182. *
  183. * @param[in] relation
  184. * The relation type which should connect the
  185. * child and parent.
  186. * @param[in] parentobject
  187. * The parent object.
  188. * @param[in] childobject
  189. * The child object.
  190. * @throw runtime_error When SQL problems occur.
  191. * @throw collison_error When the relation already exists.
  192. */
  193. virtual void addSubObjectRelation(userobject_relation_t relation,
  194. const objectid_t &parentobject, const objectid_t &childobject);
  195. /**
  196. * Delete relation between child and parent, this can be used
  197. * for example to delete a user from a group or delete
  198. * permission relations on companies.
  199. *
  200. * @param[in] relation
  201. * The relation type which connected the
  202. * child and parent.
  203. * @param[in] parentobject
  204. * The parent object.
  205. * @param[in] childobject
  206. * The child object.
  207. * @throw runtime_error When SQL problems occur.
  208. * @throw objectnotfound When the relation did not exist.
  209. */
  210. virtual void deleteSubObjectRelation(userobject_relation_t relation,
  211. const objectid_t &parentobject, const objectid_t &childobject);
  212. /**
  213. * Request quota information from object
  214. *
  215. * @param[in] id
  216. * The objectid from which the quota should be read
  217. * @param[in] bGetUserDefault
  218. * Boolean to indicate if the userdefault quota must be requested.
  219. * @return The quota details
  220. * @throw runtime_error when SQL problems occur
  221. */
  222. virtual std::unique_ptr<quotadetails_t> getQuota(const objectid_t &id, bool bGetUserDefault);
  223. /**
  224. * Update object with quota information
  225. *
  226. * @param[in] id
  227. * The object id which should be updated
  228. * @param[in] quotadetails
  229. * The quota details which must be written to the Database
  230. * @throw runtime_error when SQL problems occur
  231. */
  232. virtual void setQuota(const objectid_t &id, const quotadetails_t &quotadetails);
  233. /**
  234. * Get extra properties which are set in the object details for the addressbook
  235. *
  236. * @note It is not mandatory to implement this function
  237. *
  238. * @return a empty list of properties
  239. * @throw runtime_error when SQL problems occur
  240. */
  241. virtual std::unique_ptr<abprops_t> getExtraAddressbookProperties(void);
  242. virtual void removeAllObjects(objectid_t except);
  243. private:
  244. /**
  245. * Execute a query and return all objects in the form of object signatures
  246. *
  247. * The SQL query must request 3 columns in his order::
  248. * - externid
  249. * - objectclass
  250. * - signature
  251. * .
  252. *
  253. * @param[in] query
  254. * The SQL query which should be executed
  255. * @return The list of object signatures which were returned by the SQL query
  256. * @throw runtime_error when SQL problems occur
  257. */
  258. virtual std::unique_ptr<signatures_t> CreateSignatureList(const std::string &query);
  259. /**
  260. * Convert a string to MD5Hash
  261. * This will be used for hashing passwords
  262. *
  263. * @param[in] strData
  264. * The data which should be converted into the MD5Hash.
  265. * @param[out] lpstrResult
  266. * The MD5Hash of strData.
  267. * @return KCERR_INVALID_PARAMETER if strData is empty or lpstrResult is NULL
  268. */
  269. virtual ECRESULT CreateMD5Hash(const std::string &strData, std::string* lpstrResult);
  270. /**
  271. * Create a new object based on an objectdetails_t instance with an
  272. * externid. This happens when the object is created via ICS.
  273. *
  274. * @param[in] objectid
  275. * The objectid of the object to create
  276. * @param[in] details
  277. * The details of the object.
  278. */
  279. void CreateObjectWithExternId(const objectid_t &objectid, const objectdetails_t &details);
  280. /**
  281. * Create a new object based on an objectdetails_t instance without
  282. * an externid. This happens when the object is created with
  283. * kopano-admin.
  284. *
  285. * @param[in] details
  286. * The details of the object.
  287. * @return The new objectid
  288. */
  289. objectid_t CreateObject(const objectdetails_t &details);
  290. protected:
  291. /**
  292. * Search in the Database for all users which contain the search term.
  293. *
  294. * @param[in] match
  295. * The search term which should be found
  296. * @param[in] search_props
  297. * The Database property which should be checked during the query
  298. * @param[in] return_prop
  299. * The Database property which should be returned as externid
  300. * @param[in] ulFlags
  301. * If set to EMS_AB_ADDRESS_LOOKUP only exact matches will be found
  302. * @return The list of object signatures which match the search term
  303. * @throw objectnotfound when no results have been found
  304. */
  305. virtual std::unique_ptr<signatures_t> searchObjects(const std::string &match, const char **search_props, const char *return_prop, unsigned int ulFlags);
  306. /**
  307. * Update objectdetails with sendas information.
  308. * This will obtain the sendas relation information by calling getSubObjectsForObject()
  309. *
  310. * @param[in] objectid
  311. * The objectid for which the object details should be updated.
  312. * @param[in] lpDetails
  313. * The object details which will be updated.
  314. */
  315. virtual void addSendAsToDetails(const objectid_t &objectid, objectdetails_t *lpDetails);
  316. ECDatabase *m_lpDatabase = nullptr;
  317. };
  318. /** @} */
  319. } /* namespace */
  320. #endif