Commit 7c9c3829 authored by markus scheller's avatar markus scheller

Merge branch 'master' into 172-dokumentation-ueberarbeiten

parents bf03dc3f 130cbe7c
......@@ -5,10 +5,11 @@ from datetime import datetime
class User:
def __init__(self, username, groups, timestamp=None, obsolete=False,
permanent=False):
def __init__(self, username, groups, all_groups, timestamp=None,
obsolete=False, permanent=False):
self.username = username
self.groups = groups
self.all_groups = all_groups
if timestamp is not None:
self.timestamp = timestamp
else:
......@@ -18,21 +19,23 @@ class User:
def summarize(self):
return ":".join((
self.username, ",".join(self.groups),
self.username, ",".join(self.groups), ",".join(self.all_groups),
str(self.timestamp.timestamp()), str(self.obsolete),
str(self.permanent)))
@staticmethod
def from_summary(summary):
parts = summary.split(":", 4)
if len(parts) != 5:
parts = summary.split(":", 5)
if len(parts) != 6:
return None
name, group_str, timestamp_str, obsolete_str, permanent_str = parts
(name, group_str, all_group_str, timestamp_str, obsolete_str,
permanent_str) = parts
timestamp = datetime.fromtimestamp(float(timestamp_str))
obsolete = obsolete_str == "True"
groups = group_str.split(",")
all_groups = group_str.split(",")
permanent = permanent_str == "True"
return User(name, groups, timestamp, obsolete, permanent)
return User(name, groups, all_groups, timestamp, obsolete, permanent)
@staticmethod
def from_hashstring(secure_string):
......@@ -48,15 +51,13 @@ class UserManager:
for backend in self.backends:
if backend.authenticate(username, password):
groups = sorted(list(set(backend.groups(username, password))))
all_groups = sorted(list(set(backend.all_groups(
username, password))))
return User(
username, groups, obsolete=backend.obsolete,
username, groups, all_groups, obsolete=backend.obsolete,
permanent=permanent)
return None
def all_groups(self):
for backend in self.backends:
yield from backend.all_groups()
class SecurityManager:
def __init__(self, key, max_duration=300):
......@@ -107,7 +108,7 @@ class StaticUserManager:
if username in self.group_map:
yield from self.group_map[username]
def all_groups(self):
def all_groups(self, username, password):
yield from list(set(group for group in self.group_map.values()))
......@@ -140,7 +141,7 @@ try:
if members is not None and username in members:
yield group.cn.value
def all_groups(self):
def all_groups(self, username, password):
connection = ldap3.Connection(self.server)
obj_def = ldap3.ObjectDef("posixgroup", connection)
group_reader = ldap3.Reader(connection, obj_def, self.group_dn)
......@@ -175,33 +176,45 @@ try:
def groups(self, username, password):
connection = self.prepare_connection(username, password)
connection.bind()
if not connection.bind():
return
obj_def = ldap3.ObjectDef("user", connection)
name_filter = "cn:={}".format(username)
user_reader = ldap3.Reader(
connection, obj_def, self.user_dn, name_filter)
group_def = ldap3.ObjectDef("group", connection)
all_group_reader = ldap3.Reader(
connection, group_def, self.group_dn)
all_groups = {
group.primaryGroupToken.value: group
for group in all_group_reader.search()
}
def _yield_recursive_groups(group_dn):
group_reader = ldap3.Reader(
connection, group_def, group_dn, None)
connection, group_def, group_dn)
for entry in group_reader.search():
yield entry.name.value
for child in entry.memberOf:
yield from _yield_recursive_groups(child)
for result in user_reader.search():
yield from _yield_recursive_groups(
all_groups[result.primaryGroupID.value]
.distinguishedName.value)
for group_dn in result.memberOf:
yield from _yield_recursive_groups(group_dn)
def all_groups(self):
connection = self.prepare_connection()
connection.bind()
def all_groups(self, username, password):
connection = self.prepare_connection(username, password)
if not connection.bind():
return
obj_def = ldap3.ObjectDef("group", connection)
group_reader = ldap3.Reader(connection, obj_def, self.group_dn)
for result in group_reader.search():
yield result.name.value
except ModuleNotFoundError:
except ImportError:
pass
......@@ -219,14 +232,13 @@ try:
return self.pam.authenticate(username, password)
def groups(self, username, password=None):
print(username)
yield grp.getgrgid(pwd.getpwnam(username).pw_gid).gr_name
for group in grp.getgrall():
if username in group.gr_mem:
yield group.gr_name
def all_groups(self):
def all_groups(self, username, password):
for group in grp.getgrall():
yield group.gr_name
except ModuleNotFoundError:
except ImportError:
pass
......@@ -105,6 +105,12 @@ class ProtocolType(DatabaseModel):
return None
return candidates[0]
def get_protocols_on_date(self, protocol_date):
return [
protocol for protocol in self.protocols
if protocol.date == protocol_date
]
def has_public_view_right(self, user, check_networks=True):
return (
self.has_public_anonymous_view_right(check_networks=check_networks)
......@@ -423,7 +429,12 @@ class Protocol(DatabaseModel):
tzinfo=tz.tzlocal())
@staticmethod
def create_new_protocol(protocoltype, date, start_time=None):
def create_new_protocol(
protocoltype, date, start_time=None, allow_duplicate=False):
if not allow_duplicate:
duplicate_candidates = protocoltype.get_protocols_on_date(date)
if duplicate_candidates:
return duplicate_candidates[0]
if start_time is None:
start_time = protocoltype.usual_time
protocol = Protocol(
......
......@@ -75,7 +75,7 @@ try:
"release": get_git_revision(),
}
sentry.get_user_info = get_user_info
except ModuleNotFoundError:
except ImportError:
print("Raven not installed. Not sending issues to Sentry.")
except AttributeError:
print("DSN not configured. Not sending issues to Sentry.")
......@@ -91,7 +91,7 @@ def make_celery(app, config):
raven_client = RavenClient(config.SENTRY_DSN)
register_logger_signal(raven_client)
register_signal(raven_client)
except ModuleNotFoundError:
except ImportError:
print("Raven not installed. Not sending celery issues to Sentry.")
except AttributeError:
print("DSN not configured. Not sending celery issues to Sentry.")
......@@ -1029,7 +1029,7 @@ def send_protocol_reminder(protocol):
if not config.MAIL_ACTIVE:
flash("Die Mailfunktion ist nicht aktiviert.", "alert-error")
return back.redirect("show_protocol", protocol_id=protocol.id)
meetingreminders = protocol.reminders
meetingreminders = protocol.protocoltype.reminders
if len(meetingreminders) == 0:
flash("Für diesen Protokolltyp sind keine Einladungsmails "
"konfiguriert.", "alert-error")
......@@ -1924,7 +1924,8 @@ def check_and_send_reminders():
with app.app_context():
current_time = datetime.now()
current_day = current_time.date()
for protocol in Protocol.query.filter(not Protocol.done).all():
query = Protocol.query.filter(Protocol.done == False) # noqa: E712
for protocol in query.all():
day_difference = (protocol.date - current_day).days
usual_time = protocol.get_time()
protocol_time = datetime(
......
......@@ -492,8 +492,9 @@ def parse_protocol_async_inner(protocol):
if len(protocol_tag.values) > 1:
new_protocol_time = datetime.strptime(
protocol_tag.values[1], "%H:%M")
Protocol.create_new_protocol(
protocol.protocoltype, new_protocol_date, new_protocol_time)
if not protocol.protocoltype.get_protocols_on_date(new_protocol_date):
Protocol.create_new_protocol(
protocol.protocoltype, new_protocol_date, new_protocol_time)
# TOPs
old_tops = list(protocol.tops)
......@@ -582,17 +583,18 @@ def push_to_wiki(protocol, content, infobox_content, summary):
@celery.task
def push_to_wiki_async(protocol_id, content, infobox_content, summary):
with WikiClient() as wiki_client, app.app_context():
with app.app_context():
protocol = Protocol.query.filter_by(id=protocol_id).first()
try:
wiki_client.edit_page(
title=protocol.protocoltype.get_wiki_infobox_title(),
content=infobox_content,
summary=summary)
wiki_client.edit_page(
title=protocol.get_wiki_title(),
content=content,
summary=summary)
with WikiClient() as wiki_client:
wiki_client.edit_page(
title=protocol.protocoltype.get_wiki_infobox_title(),
content=infobox_content,
summary=summary)
wiki_client.edit_page(
title=protocol.get_wiki_title(),
content=content,
summary=summary)
except WikiException as exc:
return _make_error(
protocol, "Pushing to Wiki", "Pushing to Wiki failed.",
......
......@@ -82,7 +82,7 @@ def get_latex_template_choices():
def get_group_choices():
user = current_user()
groups = sorted(user.groups)
groups = sorted(user.all_groups)
choices = list(zip(groups, groups))
choices.insert(0, ("", "Keine Gruppe"))
return choices
......
import requests
from json import JSONDecodeError
import config
......@@ -117,7 +118,10 @@ class WikiClient:
"HTTP status code {} on action {}.".format(
req.status_code, action))
self.cookies.update(req.cookies)
return req.json()
try:
return req.json()
except JSONDecodeError:
raise WikiException("Server did not return valid JSON.")
def main():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment