Commit 9075633b authored by Gero Müller's avatar Gero Müller
Browse files

change to unicodetext, fix user email handling, add max_user config

parent dc623a36
......@@ -47,6 +47,9 @@ registration.mail_hosts = []
registration.sendmail = False
registration.subject = Your registration at VISPA
# maximum allowed number of users, 0 = unlimited
registration.max_users = 0
# retrieval of forgotten passwords
forgot.use = False
forgot.subject = Your VISPA password
......
......@@ -78,11 +78,14 @@ class RootController(AbstractController):
return preferences
else:
parsed_preferences = {}
for key, value in preferences.items():
parsed_preference = {}
for key2, value2 in value.items():
parsed_preference[key2] = JSON.loads(value2)
parsed_preferences[key] = JSON.dumps(parsed_preference)
try:
for key, value in preferences.items():
parsed_preference = {}
for key2, value2 in value.items():
parsed_preference[key2] = JSON.loads(value2)
parsed_preferences[key] = JSON.dumps(parsed_preference)
except:
logger.exception("parse prefernces")
return parsed_preferences
def workspace_data(self, workspace=None, keys=None):
......
"""remove unicode length
Revision ID: 17d572bdfd2d
Revises: 2c7093ed3ed6
Create Date: 2014-09-12 11:59:28.725938
"""
from sqlalchemy.types import Unicode, UnicodeText
# revision identifiers, used by Alembic.
revision = '17d572bdfd2d'
down_revision = '2c7093ed3ed6'
from alembic import op
import sqlalchemy as sa
def upgrade():
try:
op.alter_column("user", "name", type_=UnicodeText, existing_type=Unicode(30))
op.alter_column("user", "password", type_=UnicodeText, existing_type=Unicode(128))
op.alter_column("user", "email", type_=UnicodeText, existing_type=Unicode(100))
op.alter_column("user", "hash", type_=UnicodeText, existing_type=Unicode(100))
op.alter_column("workspace", "name", type_=UnicodeText, existing_type=Unicode(100))
op.alter_column("workspace", "host", type_=UnicodeText, existing_type=Unicode(100))
op.alter_column("workspace", "login", type_=UnicodeText, existing_type=Unicode(100))
op.alter_column("vispa_preferences", "key", type_=UnicodeText, existing_type=Unicode(64))
op.alter_column("vispa_preferences", "value", type_=UnicodeText, existing_type=Unicode(300))
op.alter_column("extension_preferences", "key", type_=UnicodeText, existing_type=Unicode(64))
op.alter_column("extension_preferences", "value", type_=UnicodeText, existing_type=Unicode(300))
op.alter_column("vispa_shortcuts", "key", type_=UnicodeText, existing_type=Unicode(64))
op.alter_column("vispa_shortcuts", "value", type_=UnicodeText, existing_type=Unicode(300))
op.alter_column("extension_shortcuts", "key", type_=UnicodeText, existing_type=Unicode(64))
op.alter_column("extension_shortcuts", "value", type_=UnicodeText, existing_type=Unicode(300))
except:
pass
def downgrade():
try:
op.alter_column("user", "name", type_=Unicode(30), existing_type=UnicodeText)
op.alter_column("user", "password", type_=Unicode(128), existing_type=UnicodeText)
op.alter_column("user", "email", type_=Unicode(100), existing_type=UnicodeText)
op.alter_column("user", "hash", type_=Unicode(100), existing_type=UnicodeText)
op.alter_column("workspace", "name", type_=Unicode(100), existing_type=UnicodeText)
op.alter_column("workspace", "host", type_=Unicode(100), existing_type=UnicodeText)
op.alter_column("workspace", "login", type_=Unicode(100), existing_type=UnicodeText)
op.alter_column("vispa_preferences", "key", type_=Unicode(64), existing_type=UnicodeText)
op.alter_column("vispa_preferences", "value", type_=Unicode(300), existing_type=UnicodeText)
op.alter_column("extension_preferences", "key", type_=Unicode(64), existing_type=UnicodeText)
op.alter_column("extension_preferences", "value", type_=Unicode(300), existing_type=UnicodeText)
op.alter_column("vispa_shortcuts", "key", type_=Unicode(64), existing_type=UnicodeText)
op.alter_column("vispa_shortcuts", "value", type_=Unicode(300), existing_type=UnicodeText)
op.alter_column("extension_shortcuts", "key", type_=Unicode(64), existing_type=UnicodeText)
op.alter_column("extension_shortcuts", "value", type_=Unicode(300), existing_type=UnicodeText)
except:
pass
\ No newline at end of file
......@@ -2,7 +2,7 @@
# imports
from sqlalchemy import Column, schema
from sqlalchemy.types import Integer, Unicode, DateTime
from sqlalchemy.types import Integer, Unicode, DateTime, UnicodeText
from datetime import datetime
from vispa.models import Base, insertion_safe
from vispa import MessageException
......@@ -13,17 +13,25 @@ _all__ = ["VispaPreference", "ExtensionPreference"]
class VispaPreference(Base):
__tablename__ = "vispa_preference"
user_id = Column(Integer, schema.ForeignKey("user.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False, primary_key=True)
section = Column(Unicode(64), nullable=False, primary_key=True)
value = Column(Unicode(300), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__tablename__ = "vispa_preference"
user_id = Column(
Integer,
schema.ForeignKey(
"user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False,
primary_key=True)
section = Column(UnicodeText(), nullable=False, primary_key=True)
value = Column(UnicodeText(), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__table_args__ = (schema.PrimaryKeyConstraint(user_id, section),)
@staticmethod
def get_by_user_id(session, user_id, section=None):
preferences = session.query(VispaPreference).filter_by(user_id=user_id).all()
preferences = session.query(
VispaPreference).filter_by(user_id=user_id).all()
if not section:
return preferences
for preference in preferences:
......@@ -35,7 +43,10 @@ class VispaPreference(Base):
@staticmethod
def get_data_by_user_id(session, user_id, section=None, parse_json=False):
data = {}
preferences = VispaPreference.get_by_user_id(session, user_id, section=section)
preferences = VispaPreference.get_by_user_id(
session,
user_id,
section=section)
if isinstance(preferences, list):
for preference in preferences:
if parse_json:
......@@ -54,35 +65,53 @@ class VispaPreference(Base):
def set_value(session, user_id, section, value, update=True):
safe, key = insertion_safe(section, value)
if not safe:
raise MessageException("Couldn't update preference '%s'!" % section)
raise MessageException(
"Couldn't update preference '%s'!" %
section)
# entry already exists?
preference = VispaPreference.get_by_user_id(session, user_id, section=section)
preference = VispaPreference.get_by_user_id(
session,
user_id,
section=section)
if isinstance(preference, VispaPreference):
preference.value = value
if update:
preference.timestmap = datetime.now()
else:
preference = VispaPreference(user_id=user_id, section=section, value=value)
preference = VispaPreference(
user_id=user_id,
section=section,
value=value)
session.add(preference)
session.commit()
if not isinstance(preference, VispaPreference):
raise MessageException("Couldn't update preference '%s'!" % section)
raise MessageException(
"Couldn't update preference '%s'!" %
section)
return preference
class ExtensionPreference(Base):
__tablename__ = "extension_preference"
user_id = Column(Integer, schema.ForeignKey("user.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False, primary_key=True)
key = Column(Unicode(64), nullable=False, primary_key=True)
value = Column(Unicode(300), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__tablename__ = "extension_preference"
user_id = Column(
Integer,
schema.ForeignKey(
"user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False,
primary_key=True)
key = Column(Unicode(64), nullable=False, primary_key=True)
value = Column(Unicode(300), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__table_args__ = (schema.PrimaryKeyConstraint(user_id, key),)
@staticmethod
def get_by_user_id(session, user_id, key=None):
preferences = session.query(ExtensionPreference).filter_by(user_id=user_id).all()
preferences = session.query(
ExtensionPreference).filter_by(user_id=user_id).all()
if not key:
return preferences
for preference in preferences:
......@@ -94,7 +123,10 @@ class ExtensionPreference(Base):
@staticmethod
def get_data_by_user_id(session, user_id, key=None, parse_json=False):
data = {}
preferences = ExtensionPreference.get_by_user_id(session, user_id, key=key)
preferences = ExtensionPreference.get_by_user_id(
session,
user_id,
key=key)
if isinstance(preferences, list):
for preference in preferences:
if parse_json:
......@@ -115,13 +147,19 @@ class ExtensionPreference(Base):
if not safe:
raise MessageException("Couldn't update preference '%s'!" % key)
# entry already exists?
preference = ExtensionPreference.get_by_user_id(session, user_id, key=key)
preference = ExtensionPreference.get_by_user_id(
session,
user_id,
key=key)
if isinstance(preference, ExtensionPreference):
preference.value = value
if update:
preference.timestmap = datetime.now()
else:
preference = ExtensionPreference(user_id=user_id, key=key, value=value)
preference = ExtensionPreference(
user_id=user_id,
key=key,
value=value)
session.add(preference)
session.commit()
if not isinstance(preference, ExtensionPreference):
......
......@@ -2,7 +2,7 @@
# imports
from sqlalchemy import Column, schema
from sqlalchemy.types import Integer, Unicode, DateTime
from sqlalchemy.types import Integer, Unicode, DateTime, UnicodeText
from datetime import datetime
from vispa.models import Base, insertion_safe
import json as JSON
......@@ -12,17 +12,25 @@ __all__ = ["VispaShortcuts", "ExtensionShortcuts"]
class VispaShortcuts(Base):
__tablename__ = "vispa_shortcuts"
user_id = Column(Integer, schema.ForeignKey("user.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False, primary_key=True)
key = Column(Unicode(64), nullable=False, primary_key=True)
value = Column(Unicode(300), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__tablename__ = "vispa_shortcuts"
user_id = Column(
Integer,
schema.ForeignKey(
"user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False,
primary_key=True)
key = Column(UnicodeText(), nullable=False, primary_key=True)
value = Column(UnicodeText(), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__table_args__ = (schema.PrimaryKeyConstraint(user_id, key),)
@staticmethod
def get_by_user_id(session, user_id, key=None):
all_shortcuts = session.query(VispaShortcuts).filter_by(user_id=user_id).all()
all_shortcuts = session.query(
VispaShortcuts).filter_by(user_id=user_id).all()
if not key:
return all_shortcuts
for shortcuts in all_shortcuts:
......@@ -34,7 +42,10 @@ class VispaShortcuts(Base):
@staticmethod
def get_data_by_user_id(session, user_id, key=None, parse_json=False):
data = {}
all_shortcuts = VispaShortcuts.get_by_user_id(session, user_id, key=key)
all_shortcuts = VispaShortcuts.get_by_user_id(
session,
user_id,
key=key)
if isinstance(all_shortcuts, list):
for shortcuts in all_shortcuts:
if parse_json:
......@@ -55,13 +66,19 @@ class VispaShortcuts(Base):
if not safe:
raise MessageException("Couldn't update shortcuts '%s'!" % key)
# entry already exists?
all_shortcuts = VispaShortcuts.get_by_user_id(session, user_id, key=key)
all_shortcuts = VispaShortcuts.get_by_user_id(
session,
user_id,
key=key)
if isinstance(all_shortcuts, VispaShortcuts):
all_shortcuts.value = value
if update:
all_shortcuts.timestmap = datetime.now()
else:
all_shortcuts = VispaShortcuts(user_id=user_id, key=key, value=value)
all_shortcuts = VispaShortcuts(
user_id=user_id,
key=key,
value=value)
session.add(all_shortcuts)
session.commit()
if not isinstance(all_shortcuts, VispaShortcuts):
......@@ -71,17 +88,25 @@ class VispaShortcuts(Base):
class ExtensionShortcuts(Base):
__tablename__ = "extension_shortcuts"
user_id = Column(Integer, schema.ForeignKey("user.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False, primary_key=True)
key = Column(Unicode(64), nullable=False, primary_key=True)
value = Column(Unicode(300), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__tablename__ = "extension_shortcuts"
user_id = Column(
Integer,
schema.ForeignKey(
"user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False,
primary_key=True)
key = Column(Unicode(64), nullable=False, primary_key=True)
value = Column(Unicode(300), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__table_args__ = (schema.PrimaryKeyConstraint(user_id, key),)
@staticmethod
def get_by_user_id(session, user_id, key=None):
all_shortcuts = session.query(ExtensionShortcuts).filter_by(user_id=user_id).all()
all_shortcuts = session.query(
ExtensionShortcuts).filter_by(user_id=user_id).all()
if not key:
return all_shortcuts
for shortcuts in all_shortcuts:
......@@ -93,7 +118,10 @@ class ExtensionShortcuts(Base):
@staticmethod
def get_data_by_user_id(session, user_id, key=None, parse_json=False):
data = {}
all_shortcuts = ExtensionShortcuts.get_by_user_id(session, user_id, key=key)
all_shortcuts = ExtensionShortcuts.get_by_user_id(
session,
user_id,
key=key)
if isinstance(all_shortcuts, list):
for shortcuts in all_shortcuts:
if parse_json:
......@@ -114,13 +142,19 @@ class ExtensionShortcuts(Base):
if not safe:
raise MessageException("Couldn't update shortcuts '%s'!" % key)
# entry already exists?
all_shortcuts = ExtensionShortcuts.get_by_user_id(session, user_id, key=key)
all_shortcuts = ExtensionShortcuts.get_by_user_id(
session,
user_id,
key=key)
if isinstance(all_shortcuts, ExtensionShortcuts):
all_shortcuts.value = value
if update:
all_shortcuts.timestmap = datetime.now()
else:
all_shortcuts = ExtensionShortcuts(user_id=user_id, key=key, value=value)
all_shortcuts = ExtensionShortcuts(
user_id=user_id,
key=key,
value=value)
session.add(all_shortcuts)
session.commit()
if not isinstance(all_shortcuts, ExtensionShortcuts):
......
......@@ -7,9 +7,9 @@ import random
import uuid
from passlib.hash import sha256_crypt
from sqlalchemy import Column, Table, ForeignKey
from sqlalchemy import Column, Table, ForeignKey, func
from sqlalchemy.orm import relationship
from sqlalchemy.types import Unicode, DateTime, Integer
from sqlalchemy.types import Unicode, DateTime, Integer, UnicodeText
from vispa import MessageException
from vispa.models import Base
import vispa
......@@ -29,7 +29,6 @@ class User(Base):
INACTIVE = 0
ACTIVE = 1
MAX_USERS = 500
MIN_PW_LENGTH = 8
NAME_LENGTH = [6, 30]
PASSWORD_RESET_DELAY = 30 # minutes
......@@ -39,14 +38,14 @@ class User(Base):
__tablename__ = 'user'
id = Column(Integer, nullable=False, primary_key=True)
name = Column(Unicode(30), nullable=False, unique=True)
password = Column(Unicode(128), nullable=False)
email = Column(Unicode(100), nullable=False, unique=True)
name = Column(UnicodeText(), nullable=False, unique=True)
password = Column(UnicodeText(), nullable=False)
email = Column(UnicodeText(), nullable=False, unique=True)
created = Column(DateTime, nullable=False, default=datetime.now)
last_request = Column(DateTime, nullable=False, default=datetime.now)
last_password_reset = Column(DateTime, nullable=True, default=None)
status = Column(Integer, nullable=False, default=ACTIVE)
hash = Column(Unicode(100))
hash = Column(UnicodeText())
@staticmethod
def get_by_id(session, uid):
......@@ -69,7 +68,8 @@ class User(Base):
@staticmethod
def get_by_email(session, email):
return session.query(User).filter_by(email=email).first()
return session.query(User).filter(
func.lower(email) == func.lower(email)).first()
@staticmethod
def get_by_hash(session, hash):
......@@ -126,10 +126,11 @@ class User(Base):
while User.get_by_name(session, name) is not None:
name = "Guest-" + str(uuid.uuid4())[:23]
# max users?
count = session.query(User).count()
if count >= User.MAX_USERS:
return 'The maximum number of registered users is reached!'
max_users = vispa.config('user', 'registration.max_users', 0)
if max_users > 0:
count = session.query(User).count()
if count >= max_users:
return 'The maximum number of registered users is reached!'
password = str(uuid.uuid4())
......@@ -160,19 +161,24 @@ class User(Base):
raise MessageException('Username or mail address already exists!')
# max users?
count = session.query(User).count()
if count >= User.MAX_USERS:
return 'The maximum number of registered users is reached!'
max_users = vispa.config('user', 'registration.max_users', 0)
if max_users > 0:
count = session.query(User).count()
if count >= max_users:
return 'The maximum number of registered users is reached!'
# email valid?
email = email.lower()
valid_hosts = vispa.config('user', 'registration.mail_hosts', [])
valid_hosts = [
x.lower for x in vispa.config(
'user',
'registration.mail_hosts',
[])]
emailvalid = True
emailparts = email.split("@")
if len(emailparts) != 2:
emailvalid = False
else:
host = emailparts[1]
host = emailparts[1].lower()
if len(valid_hosts) and host not in valid_hosts:
emailvalid = False
if not emailvalid:
......
......@@ -20,9 +20,9 @@ class Workspace(Base):
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=True)
name = Column(Unicode(100), nullable=False)
host = Column(Unicode(100), nullable=False)
login = Column(Unicode(100), nullable=True, default=None)
name = Column(UnicodeText, nullable=False)
host = Column(UnicodeText, nullable=False)
login = Column(UnicodeText, nullable=True, default=None)
key = Column(Text, nullable=True, default=None)
command = Column(Text, nullable=True, default=None)
created = Column(DateTime, nullable=True, default=datetime.now)
......
Supports Markdown
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