# HG changeset patch # User Marcin Kuzminski # Date 1290025742 -3600 # Node ID 26237de9b61350b17ac585981ad4ab2cab78f717 # Parent 9c1ed03ef5dba7f55eea950ad5055ee16a24bc20 Added settings model, and Exceptions lib. Filled settings with ldap settings. Extended user db models with is_ldap flag, and added easier construction of RhodeCode db settings diff -r 9c1ed03ef5db -r 26237de9b613 rhodecode/lib/db_manage.py --- a/rhodecode/lib/db_manage.py Wed Nov 17 21:23:32 2010 +0100 +++ b/rhodecode/lib/db_manage.py Wed Nov 17 21:29:02 2010 +0100 @@ -176,16 +176,13 @@ paths.ui_value = path - hgsettings1 = RhodeCodeSettings() + hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication') + hgsettings2 = RhodeCodeSettings('title', 'RhodeCode') - hgsettings1.app_settings_name = 'realm' - hgsettings1.app_settings_value = 'RhodeCode authentication' - - hgsettings2 = RhodeCodeSettings() - hgsettings2.app_settings_name = 'title' - hgsettings2.app_settings_value = 'RhodeCode' try: + + self.sa.add(hooks1) self.sa.add(hooks2) self.sa.add(hooks3) @@ -197,6 +194,12 @@ self.sa.add(paths) self.sa.add(hgsettings1) self.sa.add(hgsettings2) + for k in ['ldap_active', 'ldap_host', 'ldap_port', 'ldap_ldaps', + 'ldap_dn_user', 'ldap_dn_pass', 'ldap_base_dn']: + + setting = RhodeCodeSettings(k, '') + self.sa.add(setting) + self.sa.commit() except: self.sa.rollback() diff -r 9c1ed03ef5db -r 26237de9b613 rhodecode/lib/exceptions.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rhodecode/lib/exceptions.py Wed Nov 17 21:29:02 2010 +0100 @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# encoding: utf-8 +# Custom Exceptions modules +# Copyright (C) 2009-2010 Marcin Kuzminski +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 2 +# of the License or (at your opinion) any later version of the license. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. +""" +Created on Nov 17, 2010 +Custom Exceptions modules +@author: marcink +""" + +class UsernameError(Exception):pass +class PasswordError(Exception):pass +class ConnectionError(Exception):pass +class LdapImportError(Exception):pass diff -r 9c1ed03ef5db -r 26237de9b613 rhodecode/model/db.py --- a/rhodecode/model/db.py Wed Nov 17 21:23:32 2010 +0100 +++ b/rhodecode/model/db.py Wed Nov 17 21:29:02 2010 +0100 @@ -13,6 +13,14 @@ app_settings_name = Column("app_settings_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) app_settings_value = Column("app_settings_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) + def __init__(self, k, v): + self.app_settings_name = k + self.app_settings_value = v + + def __repr__(self): + return "" % (self.app_settings_name, + self.app_settings_value) + class RhodeCodeUi(Base): __tablename__ = 'rhodecode_ui' __table_args__ = {'useexisting':True} @@ -35,9 +43,10 @@ lastname = Column("lastname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) email = Column("email", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) last_login = Column("last_login", DATETIME(timezone=False), nullable=True, unique=None, default=None) + is_ldap = Column("is_ldap", BOOLEAN(), nullable=False, unique=None, default=False) - user_log = relation('UserLog') - user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id") + user_log = relation('UserLog', cascade='all') + user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') @LazyProperty def full_contact(self): diff -r 9c1ed03ef5db -r 26237de9b613 rhodecode/model/settings.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rhodecode/model/settings.py Wed Nov 17 21:29:02 2010 +0100 @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# encoding: utf-8 +# Model for RhodeCode settings +# Copyright (C) 2009-2010 Marcin Kuzminski +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 2 +# of the License or (at your opinion) any later version of the license. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. +""" +Created on Nov 17, 2010 +Model for RhodeCode +@author: marcink +""" +from rhodecode.lib import helpers as h +from rhodecode.model import meta +from rhodecode.model.caching_query import FromCache +from rhodecode.model.db import RhodeCodeSettings +from sqlalchemy.orm import joinedload +from sqlalchemy.orm.session import make_transient +import logging + +log = logging.getLogger(__name__) + +class SettingsModel(object): + """ + Settings model + """ + + def __init__(self): + self.sa = meta.Session() + + + def get(self, settings_key, cache=False): + r = self.sa.query(RhodeCodeSettings)\ + .filter(RhodeCodeSettings.app_settings_name == settings_key).scalar() + if cache: + r = r.options(FromCache("sql_cache_short", + "get_setting_%s" % settings_key)) + return r + + + def get_ldap_settings(self): + + r = self.sa.query(RhodeCodeSettings)\ + .filter(RhodeCodeSettings.app_settings_name\ + .startswith('ldap_'))\ + .all() + + fd = {} + + for row in r: + v = row.app_settings_value + if v in ['0', '1']: + v = v == '1' + fd.update({row.app_settings_name:v}) + + return fd