Add Page.get_perms() + more material icons

This commit is contained in:
Yusur 2023-05-16 22:38:21 +02:00
parent 0801e841ad
commit a24386c45e
5 changed files with 46 additions and 10 deletions

42
app.py
View file

@ -161,12 +161,23 @@ class User(BaseModel):
def is_authenticated(self): def is_authenticated(self):
return True return True
@property
def groups(self): def groups(self):
return ( return (
UserGroup.select().join(UserGroupMembership, on=UserGroupMembership.group) UserGroup.select().join(UserGroupMembership, on=UserGroupMembership.group)
.where(UserGroupMembership.user == self) .where(UserGroupMembership.user == self)
) )
# page perms (used as bitmasks)
PERM_READ = 1
PERM_EDIT = 2
PERM_CREATE = 4
PERM_SET_URL = 8
PERM_SET_TAGS = 16
PERM_ALL = 31
PERM_LOCK = ~(PERM_EDIT | PERM_CREATE | PERM_SET_URL | PERM_SET_TAGS)
class UserGroup(BaseModel): class UserGroup(BaseModel):
name = CharField(32, unique=True) name = CharField(32, unique=True)
permissions = BitField() permissions = BitField()
@ -250,15 +261,38 @@ class Page(BaseModel):
def prop(self): def prop(self):
return PagePropertyDict(self) return PagePropertyDict(self)
def is_editable(self): def is_editable(self):
return not self.is_locked return self.can_edit(current_user)
def can_edit(self, user): def can_edit(self, user):
if self.is_locked: perm = self.get_perms(user)
return user.id == self.owner.id return perm & PERM_EDIT or (self.owner == user and perm & PERM_CREATE)
return True
def is_owned_by(self, user): def is_owned_by(self, user):
return user.id == self.owner.id return user.id == self.owner.id
def get_perms(self, user=None):
if user is None:
user = current_user
if user.is_anonymous:
return UserGroup.get_default_group().permissions & PERM_LOCK
if user.is_admin:
return PERM_ALL
perm = 0
# default groups
for gr in user.groups:
perm |= gr.permissions
# page overrides
for ov in self.permission_overrides:
if ov.group in user.groups:
perm |= ov.permissions
if self.is_locked and self.owner.id != user.id:
perm &= PERM_LOCK
return perm
class PageText(BaseModel): class PageText(BaseModel):
content = BlobField() content = BlobField()

View file

@ -58,9 +58,9 @@
<div class="footer-copyright">&copy; 20202023 Sakuragasaki46.</div> <div class="footer-copyright">&copy; 20202023 Sakuragasaki46.</div>
<div class="footer-loggedinas"> <div class="footer-loggedinas">
{% if current_user.is_authenticated %} {% if current_user.is_authenticated %}
{{ T('logged-in-as') }}: <strong>{{ current_user.username }}</strong> <span class="material-icons">person</span> {{ T('logged-in-as') }}: <strong>{{ current_user.username }}</strong>
{% else %} {% else %}
{{ T('not-logged-in') }}. <a href="/accounts/login">{{ T("login") }}</a> <span class="material-icons">person_off</span> {{ T('not-logged-in') }}. <a href="/accounts/login">{{ T("login") }}</a>
{% endif %} {% endif %}
</div> </div>
<div class="footer-actions" id="page-actions">{% block actions %}{% endblock %}</div> <div class="footer-actions" id="page-actions">{% block actions %}{% endblock %}</div>

View file

@ -13,7 +13,7 @@
<ul> <ul>
{% if page_n > 1 %} {% if page_n > 1 %}
<li class="nl-prev"><a href="?page={{ page_n - 1}}">&laquo; Previous page</a></li> <li class="nl-prev"><a href="?page={{ page_n - 1 }}">&laquo; Previous page</a></li>
{% endif %} {% endif %}

View file

@ -7,7 +7,9 @@
<h1 id="firstHeading">{{ T('welcome').format(app_name) }}</h1> <h1 id="firstHeading">{{ T('welcome').format(app_name) }}</h1>
<div class="nl-new"> <div class="nl-new">
<a href="/create/"><button class="submit-primary">{{ T('new-note') }}</button></a> <a href="/create/">
<button class="submit-primary"><span class="material-icons">create</span> {{ T('new-note') }}</button>
</a>
</div> </div>
<h2>{{ T('latest-notes') }}</h2> <h2>{{ T('latest-notes') }}</h2>

View file

@ -18,7 +18,7 @@
<ul> <ul>
{% if page_n > 1 %} {% if page_n > 1 %}
<li class="nl-prev"><a href="?page={{ page_n - 1}}">&laquo; Previous page</a></li> <li class="nl-prev"><a href="?page={{ page_n - 1 }}">&laquo; Previous page</a></li>
{% endif %} {% endif %}
{% for u in users %} {% for u in users %}
@ -29,7 +29,7 @@
- -
Groups: Groups:
<ul class="inline"> <ul class="inline">
{% for ug in u.groups() %} {% for ug in u.groups %}
<li>{{ ug.name }}</li> <li>{{ ug.name }}</li>
{% endfor %} {% endfor %}
</ul> </ul>