PageRenderTime 21ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/globaleaks/applications/globaleaks/models/02_globaleaks.py

https://github.com/alx/GlobaLeaks
Python | 265 lines | 222 code | 13 blank | 30 comment | 14 complexity | 91f138f1c6fff602e0b402b210649a68 MD5 | raw file
  1. import randomizer
  2. import time
  3. class Globaleaks(object):
  4. """
  5. Class that contains useful CRUD methods
  6. """
  7. def __init__(self):
  8. self._db = db
  9. # I'm sorry about this, but seem that in the logic of 2_init.py
  10. # seem that 'settings' is not usable here
  11. def create_targetgroup(self, name, desc, tags, targets=None):
  12. """
  13. Creates a new targetgroup.
  14. Returns the id of the new record.
  15. """
  16. # http://zimbabwenewsonline.com/top_news/2495.html
  17. prev_row = self._db(self._db.targetgroup.name==name).select().first()
  18. if prev_row:
  19. self._db.targetgroup.update(id=prev_row.id, name=name, desc=desc,
  20. tags=tags, targets=targets)
  21. ret_id = prev_row.id
  22. else:
  23. ret_id = self._db.targetgroup.insert(name=name, desc=desc,
  24. tags=tags, targets=targets)
  25. self._db.commit()
  26. return ret_id
  27. def delete_targetgroup(self, group_id):
  28. """
  29. Deletes the targetgroup with the specified id.
  30. Returns True if the operation was successful.
  31. """
  32. result = False
  33. if self._db(self._db.targetgroup.id==group_id).select():
  34. result = True
  35. self._db(self._db.targetgroup.id==group_id).delete()
  36. self._db.commit()
  37. return result
  38. def update_targetgroup(self, group_id, **kwargs):
  39. """
  40. Changes the name field of the targetgroup with the specified id.
  41. """
  42. result = False
  43. if self._db(self._db.targetgroup.id==group_id).select():
  44. result = True
  45. self._db(self._db.targetgroup.id==group_id).update(**kwargs)
  46. self._db.commit()
  47. return result
  48. def get_group_id(self, group_name):
  49. return self._db(self._db.targetgroup.name==group_name
  50. ).select().first().id
  51. def add_to_targetgroup(self, target_id, group_id=None, group_name=None):
  52. """
  53. Adds the target with id target_id to the targetgroup with id
  54. group_id.
  55. Returns True if the operation was successful
  56. """
  57. if group_name:
  58. group_id = self.get_group_id(group_name)
  59. target_row = self._db(self._db.target.id==target_id).select().first()
  60. group_row = self._db(self._db.targetgroup.id==group_id
  61. ).select().first()
  62. result = False
  63. if target_row is not None and group_row is not None:
  64. targets_j = group_row.targets
  65. if not targets_j:
  66. # Dumps the json to the group table
  67. targets_j = json.dumps([target_id])
  68. else:
  69. tmp_j = json.loads(targets_j)
  70. tmp_j.append(target_id)
  71. targets_j = json.dumps(tmp_j)
  72. result = self._db(self._db.targetgroup.id==group_id
  73. ).update(targets=targets_j)
  74. self._db.commit()
  75. return result
  76. def remove_from_targetgroup(self, target_id, group_id):
  77. """
  78. Removes a target from a group.
  79. Returns True if the operation was successful.
  80. """
  81. target_row = self._db(self._db.target.id==target_id).select().first()
  82. group_row = self._db(self._db.targetgroup.id==group_id
  83. ).select().first()
  84. result = False
  85. if target_row is not None and group_row is not None:
  86. result = True
  87. targets_j = group_row.targets
  88. if not targets_j:
  89. targets_j = json.dumps([target_id])
  90. else:
  91. tmp = json.loads(targets_j)
  92. tmp.remove(target_id)
  93. targets_j = json.dumps(tmp)
  94. self._db(self._db.targetgroup.id==group_id
  95. ).update(targets=targets_j)
  96. self._db.commit()
  97. return result
  98. def get_targetgroups(self):
  99. """
  100. Returns a dictionary that has the targetgroup ids as keys and
  101. another dictionary as value.
  102. This second dictionary has field "data" with group data and
  103. field "members" which is a list of targets that belong to that
  104. group.
  105. """
  106. result = {}
  107. for row in self._db().select(self._db.targetgroup.ALL):
  108. result[row.id] = {}
  109. result[row.id]["data"] = dict(row)
  110. result[row.id]["members"] = []
  111. try:
  112. members = result[row.id]["data"]['targets']
  113. for member in json.loads(members):
  114. member_data = self._db(self._db.target.id==int(member)
  115. ).select().first()
  116. result[row.id]["members"].append(dict(member_data))
  117. except:
  118. result[row.id]["members"] = []
  119. return result
  120. # by default, a target is inserted with an email type only as contact_type,
  121. # in the personal page, the receiver should change that or the contact type
  122. # (eg: facebook, irc ?, encrypted mail setting up a gpg pubkey)
  123. def create_target(self, name, group_name, desc, contact_mail, could_del):
  124. """
  125. Creates a new target with the specified parameters.
  126. Returns the id of the new record.
  127. |contact_type| supported values: [email]
  128. |type| supported values: [plain*|pgp]
  129. |could_del|: true or false*, mean: could delete material
  130. |group_name|: could be specified a single group name only
  131. * = default
  132. """
  133. target_id = self._db.target.insert(name=name,
  134. desc=desc, contact_type="email",
  135. contact=contact_mail, type="plain", info="",
  136. candelete=could_del, tulip_counter=0,
  137. download_counter=0)
  138. self._db.commit()
  139. # extract the ID of the request group, if any, of found the default, if supported
  140. requested_group = group_name if group_name else settings['globals'].default_group
  141. if requested_group:
  142. group_id = self.get_group_id(requested_group)
  143. group_row = self._db(self._db.targetgroup.id==group_id).select().first()
  144. if group_row['targets']:
  145. comrades = json.loads(group_row['targets'])
  146. comrades.append(target_id)
  147. self.update_targetgroup(group_id, targets=json.dumps(comrades))
  148. else:
  149. starting_json = '["' + str(target_id) + '"]'
  150. self.update_targetgroup(group_id, targets=starting_json)
  151. return target_id
  152. def delete_target(self, target_id):
  153. """
  154. Deletes a target.
  155. """
  156. self._db(self._db.target.id==target_id).delete()
  157. return True
  158. def delete_tulips_by_target(target_id):
  159. """
  160. Delete the tulips associated to a single target
  161. """
  162. associated_tulips = self._db().select(self._db.tulip.target_id==target_id)
  163. tulips_removed = 0
  164. for single_tulip in associated_tulips:
  165. tulips_removed += 1
  166. self._db(self._db.tulip.id==single_tulip.it).delete()
  167. return tulips_removed
  168. def get_targets(self, group_set, target_set=[]):
  169. """
  170. If target_set is not a list it returns a rowset with all
  171. targets.
  172. If target_set is a list of groups it returns a rowset of targets
  173. that belong to these groups.
  174. """
  175. result_id = []
  176. if not isinstance(group_set, list):
  177. for target in self._db(self._db.target).select():
  178. result_id.append(target.id)
  179. else:
  180. rows = self._db(self._db.targetgroup).select()
  181. for row in rows:
  182. if row.id in group_set:
  183. targets = json.loads(row.targets)
  184. for t_id in targets:
  185. result_id.append(self._db(self._db.target.id==t_id
  186. ).select().first().id)
  187. result_id += target_set
  188. result = []
  189. for target_id in set(result_id):
  190. result.append(self.get_target(target_id))
  191. return result
  192. def get_target(self, target_id):
  193. """
  194. Returns the target with the specified id
  195. """
  196. return self._db(self._db.target.id==target_id).select().first()
  197. def get_target_hash(self, target_id):
  198. """
  199. Returns the target with the specified id
  200. """
  201. try:
  202. return self._db(self._db.target.id==target_id).select().first().hashpass
  203. except:
  204. return False
  205. def create_tulip(self, leak_id, target_id, hardcoded_url=None):
  206. """
  207. Creates a tulip for the target, and inserts it into the db
  208. (when target is 0, is the whitleblower and hardcoded_url is set by the caller)
  209. """
  210. if hardcoded_url and target_id:
  211. logger.error("Invalid usage of create_tulip: url and target specifyed")
  212. return NoneType
  213. tulip = self._db.tulip.insert(
  214. url= hardcoded_url if hardcoded_url else randomizer.generate_tulip_url(),
  215. leak_id=leak_id,
  216. target_id=target_id,
  217. allowed_accesses=settings.tulip.max_access,
  218. accesses_counter=0,
  219. allowed_downloads=0 if not target_id else settings.tulip.max_download,
  220. downloads_counter=0,
  221. expiry_time=settings.tulip.expire_days)
  222. self._db.commit()
  223. return tulip
  224. def get_leaks(self):
  225. pass
  226. def get_leak(self, leak_id):
  227. pass
  228. def get_tulips(self, leak_id):
  229. pass
  230. def get_tulip(self, tulip_id):
  231. pass