Skip to content

Commit

Permalink
Merge pull request #81 from mitama-org/hotfix/portal
Browse files Browse the repository at this point in the history
Hotfix/portal
  • Loading branch information
takuan517 authored Oct 5, 2020
2 parents 1331550 + 7e2a1c9 commit 727fb2f
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 32 deletions.
2 changes: 1 addition & 1 deletion mitama/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __call__(self, request):
def set_middleware(self, middlewares):
self.app.middlewares.extend(middlewares)
def convert_fullurl(self, req, url):
scheme= req.scheme
scheme = req.scheme
hostname = req.host
path = self.path
if path[0] != '/':
Expand Down
2 changes: 1 addition & 1 deletion mitama/command/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

class Command:
def handle(self, argv = None):
config = get_from_project_dir()
try:
port = argv[0]
except IndexError:
if hasattr(config, 'port'):
port = config.port
else:
port = '8080'
config = get_from_project_dir()
if not hasattr(config, 'ssl'):
config.ssl = False
app_registry = AppRegistry()
Expand Down
9 changes: 6 additions & 3 deletions mitama/http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64, re
import ssl
from http.server import ThreadingHTTPServer
#from http.server import ThreadingHTTPServer
from http.server import HTTPServer
from socketserver import StreamRequestHandler
from .request import Request
from .response import Response
Expand All @@ -19,12 +20,14 @@ def handle(self):
response = app(request)
response.start(request, self.wfile)
def serve():
with ThreadingHTTPServer(('', int(port)), RequestHandler) as server:
#with ThreadingHTTPServer(('', int(port)), RequestHandler) as server:
with HTTPServer(('', int(port)), RequestHandler) as server:
server.serve_forever()
th_nossl = threading.Thread(name = 'nossl', target = serve)
if ssl != False:
def ssl_serve():
with ThreadingHTTPServer(('', int(ssl['port'])), SSLRequestHandler) as server:
#with ThreadingHTTPServer(('', int(ssl['port'])), SSLRequestHandler) as server:
with HTTPServer(('', int(ssl['port'])), SSLRequestHandler) as server:
if ssl!=False:
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
crtx.load_cert_chain(ssl['cert'], ssl['key'])
Expand Down
41 changes: 32 additions & 9 deletions mitama/http/request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cgi
import http
import http.cookies
import io
from urllib.parse import parse_qs
from yarl import URL

Expand All @@ -21,13 +22,27 @@ def get(self, key):
class _RequestPayload():
def __init__(self, field_storage):
self._field_storage = field_storage
def __contains__(self, key):
if key not in self._field_storage:
return False
elif isinstance(self._field_storage[key].file, io.BytesIO):
return (self._field_storage[key].filename or '') != ''
else:
return len(self._field_storage[key].value) > 0
def __getitem__(self, key):
if key not in self._field_storage:
raise KeyError
elif self._field_storage[key].file is None:
elif (self._field_storage[key].filename or '') != '':
return self._field_storage[key]
elif len(self._field_storage[key].value) > 0:
return self._field_storage[key].value
else:
return self._field_storage[key]
raise KeyError
def get(self, key, default = None):
if key in self:
return self[key]
else:
return default
@classmethod
def parse_body(cls, rfile, content_type, length):
environ = {
Expand All @@ -43,7 +58,7 @@ def parse_body(cls, rfile, content_type, length):

class Request():
MessageClass = http.client.HTTPMessage
def __init__(self, method, path, version, headers, rfile):
def __init__(self, method, path, version, headers, ssl, rfile):
self._method = method
self._raw_path = path
self._url = URL(path)
Expand All @@ -60,9 +75,11 @@ def __init__(self, method, path, version, headers, rfile):
self._query[k] = v
else:
self._path = path[0]
self._query = None
self._query = {}
self._version = version
self._headers = headers
self._secure = ssl
self._host = self._headers.get('Host')
self._rfile = rfile
self._data = dict()
def __setitem__(self, key, value):
Expand All @@ -74,6 +91,12 @@ def __delitem__(self, key):
def get(self, key):
return self._data[key] if key in self._data else None
@property
def scheme(self):
return 'https' if self._secure else 'http'
@property
def host(self):
return self._host
@property
def method(self):
return self._method
@property
Expand Down Expand Up @@ -106,9 +129,9 @@ def cookies(self):
self._cookies = _Cookies(C)
return self._cookies
def post(self):
content_type = self._headers.get('Content-Type')
length = int(self._headers.get('Content-Length'))
if content_type in ('multipart/form-data', 'application/x-www-form-urlencoded'):
content_type = self._headers.get('Content-Type', '')
length = int(self._headers.get('Content-Length', 0))
if content_type.startswith('multipart/form-data') or content_type.startswith('application/x-www-form-urlencoded'):
parsed_body = _RequestPayload.parse_body(self._rfile, content_type, length)
elif content_type == 'application/json':
payload = self._rfile.read()
Expand All @@ -128,7 +151,7 @@ def session(self):
self['mitama_session'] = sess
return sess
@classmethod
def parse_stream(cls, rfile):
def parse_stream(cls, rfile, ssl = False):
words = rfile.readline().decode().rstrip('\r\n').split(' ')
version = words[-1]
if not 2 <= len(words) <= 3:
Expand All @@ -144,4 +167,4 @@ def parse_stream(cls, rfile):
raise Exception('')
except http.client.HTTPException as err:
raise Exception('')
return cls(command, path, version, headers, rfile)
return cls(command, path, version, headers, ssl, rfile)
8 changes: 8 additions & 0 deletions mitama/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from mitama.nodes import User, Group, Relation
from mitama.db.types import Column, Integer, Node
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.schema import UniqueConstraint

class PermissionMixin(object):
'''パーミッションのモデルの実装を支援します
Expand Down Expand Up @@ -34,6 +35,13 @@ class SomePermission(PermissionMixin, db.Model):
@declared_attr
def __tablename__(cls):
return '__'+cls.__name__.lower()+'_permission'
@declared_attr
def __table_args__(cls):
if hasattr(cls, 'target'):
unique = UniqueConstraint('node', 'target', name='unique')
else:
unique = UniqueConstraint('node', name='unique')
return (unique, )
node = Column(Node)
targetUpPropagate = False
targetDownPropagate = False
Expand Down
36 changes: 20 additions & 16 deletions mitama/portal/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ def signup(self, request):
user = User()
user.password = password_hash(data['password'])
if invite.editable:
user.screen_name = data['screen_name']
user.name = data['name']
user.screen_name = data.get('screen_name', '')
user.name = data.get('name', '')
user.icon = data['icon'].file.read() if "icon" in data else invite.icon
else:
user.screen_name = invite.screen_name
user.name = invite.name
user.icon = invite.icon
user.create()
invite.delete()
UpdateUserPermission.accept(user, user)
sess["jwt_token"] = get_jwt(user)
return Response.redirect(
Expand All @@ -69,10 +70,10 @@ def signup(self, request):
error = str(err)
return Response.render(template, {
'error': error,
"name": data["name"],
"screen_name": data["screen_name"],
"password": data["password"],
"icon": data["icon"].file.read(),
"name": data.get("name", invite.name),
"screen_name": data.get("screen_name", invite.screen_name),
"password": data.get("password", ''),
"icon": data.get("icon").file.read(),
'editable': invite.editable
})
return Response.render(template, {
Expand Down Expand Up @@ -130,8 +131,8 @@ def create(self, req):
try:
icon = post["icon"].file.read() if "icon" in post else load_noimage_user()
invite = Invite()
invite.name = post['name']
invite.screen_name = post['screen_name']
invite.name = post.get('name', '')
invite.screen_name = post.get('screen_name', '')
invite.icon = icon
invite.token = str(uuid4())
invite.editable = 'editable' in post
Expand All @@ -145,8 +146,8 @@ def create(self, req):
error = str(err)
return Response.render(template, {
'invites': invites,
"name": post["name"],
"screen_name": post["screen_name"],
"name": post.get("name", ''),
"screen_name": post.get("screen_name", ''),
"icon": icon,
'error': error
})
Expand Down Expand Up @@ -221,8 +222,8 @@ def update(self, req):
return Response.render(template, {
"error": error,
"user": user,
"screen_name": post["screen_name"],
"name": post["name"],
"screen_name": post.get("screen_name", user.screen_name),
"name": post.get("name", user.name),
"icon": icon,
})
return Response.render(template, {
Expand Down Expand Up @@ -258,16 +259,19 @@ def create(self, req):
group.screen_name = post['screen_name']
group.icon = post['icon'].file.read() if "icon" in post else None
group.create()
print('created')
if "parent" in post and post['parent'] != '':
Group.retrieve(int(post['parent'])).append(group)
group.append(req.user)
print('append')
UpdateGroupPermission.accept(req.user, group)
print('accept')
return Response.redirect(self.app.convert_url("/groups"))
except Exception as err:
error = str(err)
return Response.render(template, {
'groups': groups,
"icon": post["icon"].file.read() if "icon" in post else None,
"icon": load_noimage_group(),
'error': error
})
return Response.render(template, {
Expand Down Expand Up @@ -347,9 +351,9 @@ def update(self, req):
'all_groups': groups,
'all_users': users,
"group": group,
"screen_name": post["screen_name"],
"name": post["name"],
"icon": icon,
"screen_name": post.get("screen_name", ''),
"name": post.get("name", ''),
"icon": group.icon,
})
return Response.render(template, {
"group": group,
Expand Down
4 changes: 2 additions & 2 deletions mitama/portal/templates/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
{% block content %}
<div id='content'>
<h1>Mitamaに参加する</h1>
<form method='post' action='' enctype="multipart/form-data" class='entrance'>
<form method='POST' action='' enctype="multipart/form-data" class='entrance'>
<dl>
<dt>アイコン</dt>
<dd>
<div id='image-form'>
<img src="{{ dataurl(icon) if icon != None else url("/static/noimage_user.svg") }}"/>
<img src="{{ dataurl(icon) if icon|length > 0 else url("/static/noimage_user.svg") }}"/>
{% if editable %}
<label class='button'>
<input type='file' name='icon' accept='image/*' onchange='previewImage(this)'/>
Expand Down
5 changes: 5 additions & 0 deletions mitama/portal/templates/user/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ <h2 class='mini-title dark'>{{ user.name }}の設定</h2>
{% endif %}
</dl>
<button type='submit'>設定を変更</button>
<!--
<a href='{{ url('/users/'+user.id|string+'/delete') }}'>
<button type='button'>ユーザーを削除</button>
</a>
-->
{% if error %}
<p class='error'>{{ error }}</p>
{% endif %}
Expand Down

0 comments on commit 727fb2f

Please sign in to comment.