25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

382 lines
10 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Mir Calculate. http://www.calculate-linux.org
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import sys
  16. import re
  17. from fnmatch import fnmatch
  18. from calculate.lib.datavars import (Variable, VariableInterface, VariableError)
  19. from calculate.unix.variables.helpers import (UnixUserHelper, UnixGroupHelper,
  20. UnixActionHelper)
  21. from calculate.lib.cl_ldap import LDAPConnectError
  22. import operator
  23. from itertools import chain
  24. _ = lambda x: x
  25. from calculate.lib.cl_lang import (setLocalTranslate, getLazyLocalTranslate)
  26. setLocalTranslate('cl_unix3', sys.modules[__name__])
  27. __ = getLazyLocalTranslate(_)
  28. """
  29. String
  30. --filter-comment fasdfad faksjdflk* ---fasdfas|fasdfasd
  31. Integer
  32. --filter-uid 321 >12000 <11000
  33. --filter-uid 321, >=, <, >, >= 325-400
  34. Boolean
  35. --filter-password on/off/auto
  36. """
  37. class FilterHelper(VariableInterface):
  38. def test(self, value):
  39. """
  40. Проверка попадания значения под фильтр
  41. :param value:
  42. :return:
  43. """
  44. return True
  45. def enabled(self):
  46. filter_values = self.Get()
  47. if not filter_values:
  48. return False
  49. return True
  50. class FilterStringHelper(FilterHelper):
  51. """
  52. Для фильтрации строковых данных
  53. user10, user*, !user*, !user20
  54. """
  55. value = ""
  56. metavalue = "FILTER"
  57. def test_value(self, value):
  58. filter_values = self.Get()
  59. if not filter_values:
  60. return True
  61. for filter_value in filter_values.split('|'):
  62. revert = lambda x: x
  63. if filter_value[:1] == "!" or filter_value[:2] == r"\!":
  64. revert = lambda x: not x
  65. if filter_value[:2] == r"\!":
  66. filter_value = filter_value[2:]
  67. else:
  68. filter_value = filter_value[1:]
  69. if revert(fnmatch(value, filter_value)):
  70. return True
  71. else:
  72. return False
  73. class FilterListHelper(FilterHelper):
  74. """
  75. Для фильтрации данных со списками
  76. user10, user*, !user*, !user20
  77. """
  78. value = ""
  79. metavalue = "FILTER"
  80. def test_value(self, value):
  81. value = list(value)
  82. filter_values = self.Get()
  83. if not filter_values:
  84. return True
  85. for filter_value in filter_values.split('|'):
  86. revert = lambda x: x
  87. if filter_value[:1] == "!" or filter_value[:2] == r"\!":
  88. revert = lambda x: not x
  89. if filter_value[:2] == r"\!":
  90. filter_value = filter_value[2:]
  91. else:
  92. filter_value = filter_value[1:]
  93. results = [fnmatch(x, filter_value) for x in value]
  94. if revert(any(results)):
  95. return True
  96. else:
  97. return False
  98. class FilterIntegerHelper(FilterHelper):
  99. """
  100. Для фильтрации числовых данных
  101. >5, <5, >=5, <=5, 5, 5-10
  102. """
  103. value = ""
  104. filter_format = re.compile("^(\d+)-(\d+)|(<=|>=|<|>)?(\d+)$")
  105. metavalue = "FILTER"
  106. def test_value(self, value):
  107. filter_value = self.Get()
  108. if not filter_value:
  109. return True
  110. start, end, op, number = self.filter_format.search(
  111. filter_value).groups()
  112. if start and end:
  113. return int(start) <= value <= int(end)
  114. number = int(number)
  115. if op:
  116. return {"<=": operator.le,
  117. ">=": operator.ge,
  118. "<": operator.lt,
  119. ">": operator.gt}.get(op)(value, number)
  120. else:
  121. return value == number
  122. def check(self, value):
  123. if not self.filter_format.match(value):
  124. raise VariableError(_("Wrong filter"))
  125. class FilterBooleanHelper(FilterHelper):
  126. """
  127. Для фильтрации булевых данных
  128. """
  129. type = "boolauto"
  130. value = "auto"
  131. metavalue = "ON/OFF/AUTO"
  132. def enabled(self):
  133. filter_value = self.Get()
  134. if filter_value == "auto":
  135. return False
  136. return True
  137. def test_value(self, value):
  138. filter_value = self.Get()
  139. if filter_value == "auto":
  140. return True
  141. return Variable.isTrue(filter_value) == value
  142. class VariableClUnixUserFilterLogin(FilterStringHelper, Variable):
  143. """
  144. Фильтр на login
  145. """
  146. opt = ["--filter-login"]
  147. def init(self):
  148. self.label = _("Login filter")
  149. self.help = _("set login filter")
  150. def test(self, user):
  151. return self.test_value(user.username)
  152. class VariableClUnixUserFilterComment(FilterStringHelper, Variable):
  153. """
  154. Фильтр на полное имя пользователя
  155. """
  156. opt = ["--filter-fullname"]
  157. def init(self):
  158. self.label = _("Full name filter")
  159. self.help = _("set full name filter")
  160. def test(self, user):
  161. return self.test_value(user.comment)
  162. class VariableClUnixUserFilterHomePath(FilterStringHelper, Variable):
  163. """
  164. Фильтр на домашнюю директорию
  165. """
  166. opt = ["--filter-homedir"]
  167. def init(self):
  168. self.label = _("Home directory filter")
  169. self.help = _("set home directory filter")
  170. def test(self, user):
  171. return self.test_value(user.homedir)
  172. class VariableClUnixUserFilterShell(FilterStringHelper, Variable):
  173. """
  174. Фильтр на shell
  175. """
  176. opt = ["--filter-shell"]
  177. def init(self):
  178. self.label = _("Shell filter")
  179. self.help = _("set shell filter")
  180. def test(self, user):
  181. return self.test_value(user.shell)
  182. class VariableClUnixUserFilterGid(FilterStringHelper, UnixGroupHelper,
  183. Variable):
  184. """
  185. Фильтр на primary group
  186. """
  187. opt = ["--filter-gid"]
  188. def init(self):
  189. self.label = _("Primary group filter")
  190. self.help = _("set primary group filter")
  191. def test(self, user):
  192. primary_group = str(self.gid_to_name(str(user.gid)))
  193. return self.test_value(primary_group)
  194. def set(self, value):
  195. if value.isdigit():
  196. value = self.gid_to_name(str(value))
  197. return str(value)
  198. class VariableClUnixUserFilterGroups(FilterListHelper, UnixGroupHelper,
  199. Variable):
  200. """
  201. Фильтр на вхождение пользователя в группу
  202. """
  203. opt = ["--filter-groups"]
  204. def init(self):
  205. self.label = _("Supplementary group filter")
  206. self.help = _("set supplementary group filter")
  207. def test(self, user):
  208. return self.test_value(x.group_name for x in self.iterate_ldap_group(
  209. "memberUid=%s" % user.username))
  210. class VariableClUnixUserFilterUid(FilterIntegerHelper, Variable):
  211. """
  212. Фильтр на id пользователя
  213. """
  214. opt = ["--filter-uid"]
  215. def init(self):
  216. self.label = _("User ID filter")
  217. self.help = _("set user ID filter")
  218. def test(self, user):
  219. return self.test_value(user.uid)
  220. class VariableClUnixUserFilterPwSet(FilterBooleanHelper, Variable):
  221. """
  222. Фильтр на наличие пароля
  223. """
  224. opt = ["--filter-password"]
  225. def init(self):
  226. self.label = _("Password availability filter")
  227. self.help = _("set password availability filter")
  228. def test(self, user):
  229. return self.test_value(user.pass_set)
  230. class VariableClUnixUserFilterVisibleSet(FilterBooleanHelper, Variable):
  231. """
  232. Фильтр на видимость
  233. """
  234. opt = ["--filter-visible"]
  235. def init(self):
  236. self.label = _("Visibility filter")
  237. self.help = _("set visibility filter")
  238. def test(self, user):
  239. return self.test_value(user.visible)
  240. class VariableClUnixUserFilterLockSet(FilterBooleanHelper, Variable):
  241. """
  242. Фильтр на login
  243. """
  244. opt = ["--filter-lock"]
  245. def init(self):
  246. self.label = _("Lock filter")
  247. self.help = _("set lock filter")
  248. def test(self, user):
  249. return self.test_value(user.lock)
  250. class VariableClUnixGroupFilterName(FilterStringHelper, Variable):
  251. """
  252. Фильтр на group name
  253. """
  254. opt = ["--filter-name"]
  255. def init(self):
  256. self.label = _("Name filter")
  257. self.help = _("set name filter")
  258. def test(self, group):
  259. return self.test_value(group.group_name)
  260. class VariableClUnixGroupFilterId(FilterIntegerHelper, Variable):
  261. """
  262. Фильтр на group id
  263. """
  264. opt = ["--filter-gid"]
  265. def init(self):
  266. self.label = _("Group ID filter")
  267. self.help = _("set group ID filter")
  268. def test(self, group):
  269. return self.test_value(group.gid)
  270. class VariableClUnixGroupFilterComment(FilterStringHelper, Variable):
  271. """
  272. Фильтр на group comment
  273. """
  274. opt = ["--filter-comment"]
  275. def init(self):
  276. self.label = _("Comment filter")
  277. self.help = _("set comment filter")
  278. def test(self, group):
  279. return self.test_value(group.comment)
  280. class VariableClUnixGroupFilterUsers(FilterListHelper, UnixUserHelper,
  281. Variable):
  282. """
  283. Фильтр на наличие пользователей в группе
  284. """
  285. opt = ["--filter-users"]
  286. def init(self):
  287. self.label = _("User filter")
  288. self.help = _("set users filter")
  289. def test(self, group):
  290. return self.test_value(
  291. chain(group.user_list,
  292. (user.username for user in self.iterate_ldap_user(
  293. "gidNumber=%d" % group.gid))))