Created
October 3, 2011 23:24
-
-
Save aeris/1260531 to your computer and use it in GitHub Desktop.
Possible architecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
rhodecode.dao | |
~~~~~~~~~~~~~ | |
Data access layer | |
:created_on: Oct 4, 2011 | |
:author: nvinot | |
:copyright: (C) 2011 Nicolas VINOT <aeris@imirhil.fr> | |
:license: GPLv3, see COPYING for more details. | |
""" | |
# 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, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# 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, see <http://www.gnu.org/licenses/>. | |
from rhodecode.model.db import User, Group, UsersGroup, Permission, \ | |
UsersGroupToPerm, Repository, UsersGroupRepoToPerm | |
from rhodecode.model.meta import Session | |
class GenericDao: | |
def __init__( self, cls ): | |
self.__cls = cls | |
def save( self, obj ): | |
Session.add( obj ) | |
def update( self, obj ): | |
pass | |
def delete( self, obj ): | |
Session.delete( obj ) | |
def _query( self ): | |
return Session.query( self.__cls ) | |
def get_by_id( self, id_ ): | |
return self._query().get( id_ ); | |
def get_all( self ): | |
return self._query().all() | |
class UserDao( GenericDao ): | |
def __init__( self ): | |
GenericDao.__init__( self, User ) | |
def get_by_username( self, name ): | |
return self._query().filter( User.username == name ).scalar() | |
def get_by_apikey( self, apikey ): | |
return self._query().filter( User.api_key == apikey ).scalar() | |
class UsersGroupDao( GenericDao ): | |
def __init__( self ): | |
GenericDao.__init__( self, UsersGroup ) | |
def get_by_name( self, name ): | |
return self._query().filter( UsersGroup.users_group_name == name ).scalar() | |
class PermissionDao( GenericDao ): | |
def __init__( self ): | |
GenericDao.__init__( self, Permission ) | |
def get_by_name( self, name ): | |
return self._query().filter( Permission.permission_name == name ).one() | |
class UsersGroupPermissionDao( GenericDao ): | |
def __init__( self ): | |
GenericDao.__init__( self, UsersGroupToPerm ) | |
def get_by_group( self, group ): | |
return self._query().filter( UsersGroupToPerm.users_group == group ).one() | |
class RepositoryDao( GenericDao ): | |
def __init__( self ): | |
GenericDao.__init__( self, Repository ) | |
def get_by_name( self, name ): | |
return self._query().filter( Repository.repo_name == name ).scalar() | |
class UsersGroupRepositoryPermissionDao( GenericDao ): | |
def __init__( self ): | |
GenericDao.__init__( self, UsersGroupRepoToPerm ) | |
def get_by_repository_and_group( self, repository, group ): | |
return self._query().filter( UsersGroupRepoToPerm.repository == repository ) \ | |
.filter( UsersGroupRepoToPerm.users_group == group ) \ | |
.scalar() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
rhodecode.service | |
~~~~~~~~~~~~~~~~~ | |
Business service layer | |
:created_on: Oct 4, 2011 | |
:author: nvinot | |
:copyright: (C) 2011 Nicolas VINOT <aeris@imirhil.fr> | |
:license: GPLv3, see COPYING for more details. | |
""" | |
# 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, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# 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, see <http://www.gnu.org/licenses/>. | |
from pylons.i18n.translation import _ | |
from rhodecode.dao import UserDao, UsersGroupDao, UsersGroupPermissionDao, \ | |
RepositoryDao, UsersGroupRepositoryPermissionDao | |
from rhodecode.model.db import User, Group, UsersGroup, Repository, \ | |
UsersGroupRepoToPerm | |
from rhodecode.lib.auth import get_crypt_password, generate_api_key | |
class GenericService: | |
def __init__( self, dao_cls ): | |
self._dao = dao_cls() | |
class UserService( GenericService ): | |
def __init__( self ): | |
GenericService.__init__( self, UserDao ) | |
def create( self, username, password, firstname, lastname, email, | |
active = True, admin = False, ldap_dn = None ): | |
if self._dao.get_by_name( username ): | |
raise Exception( _( "Already existing user %(user)s" ) | |
% dict( user = username ) ) | |
user = User() | |
user.username = username | |
user.password = get_crypt_password( password ) | |
user.name = firstname | |
user.lastname = lastname | |
user.email = email | |
user.active = active | |
user.admin = admin | |
user.ldap_dn = ldap_dn | |
user.api_key = generate_api_key( username ) | |
self._dao.save( user ) | |
return user | |
def update( self, user, username, password, firstname, lastname, email, | |
active = True, admin = False, ldap_dn = None ): | |
if self._dao.get_by_name( username ): | |
raise Exception( _( "Already existing user %(user)s" ) | |
% dict( user = username ) ) | |
user.username = username | |
user.password = get_crypt_password( password ) | |
user.name = firstname | |
user.lastname = lastname | |
user.email = email | |
user.active = active | |
user.admin = admin | |
user.ldap_dn = ldap_dn | |
self._dao.save( user ) | |
return user | |
class UsersGroupService( GenericService ): | |
def __init__( self ): | |
GenericService.__init__( self, UsersGroupDao ) | |
def create( self, name, active = True ): | |
if self._dao.get_by_name( name ): | |
raise Exception( _( "Already existing users group %(group)s" ) | |
% dict( group = name ) ) | |
group = UsersGroup() | |
group.users_group_name = name | |
group.users_group_active = active | |
self._dao.save( group ) | |
return group | |
def update( self, group, name, active = True ): | |
if self._dao.get_by_name( name ): | |
raise Exception( _( "Already existing users group %(group)s" ) | |
% dict( group = name ) ) | |
group.users_group_name = name | |
group.users_group_active = active | |
self._dao.update( group ) | |
return group | |
def add( self, group, user ): | |
if user not in group.members: | |
group.members.append( user ) | |
self._dao.update( group ) | |
return group | |
def remove( self, group, user ): | |
if user in group.members: | |
group.members.remove( user ) | |
self._dao.update( group ) | |
return group | |
class RepositoryService( GenericService ): | |
def __init__( self ): | |
GenericService.__init__( self, RepositoryDao ) | |
def create( self, name, owner, description = None, type_ = 'hg', | |
private = False ): | |
if self._dao.get_by_name( name ): | |
raise Exception( _( "Already existing repository %(repo)s" ) | |
% dict( repo = name ) ) | |
repository = Repository() | |
repository.repo_name = name | |
repository.repo_type = type_ | |
repository.user = owner | |
repository.private = private | |
repository.description = description | |
self._dao.save( repository ) | |
FileSystemRepositoryService()._create( name, type_ ) | |
return repository | |
def fork( self, repository, name, owner, description = None, | |
private = False ): | |
if self._dao.get_by_name( name ): | |
raise Exception( _( "Already existing repository %(repo)s" ) | |
% dict( repo = name ) ) | |
fork = Repository() | |
fork.repo_name = name | |
fork.repo_type = repository.repo_type | |
fork.user = owner | |
fork.private = private | |
fork.description = description or repository.description | |
fork.fork = repository | |
self._dao.save( fork ) | |
FileSystemRepositoryService()._fork( repository.repo_name, | |
fork.repo_name, | |
repository.repo_type ) | |
return repository | |
def update( self, repository, name, owner, description = None, | |
private = False ): | |
if self._dao.get_by_name( name ): | |
raise Exception( _( "Already existing repository %(repo)s" ) | |
% dict( repo = name ) ) | |
old_name = repository.repo_name | |
repository.repo_name = name | |
repository.user = owner | |
repository.private = private | |
repository.description = description | |
self._dao.save( repository ) | |
if old_name != name: | |
FileSystemRepositoryService()._rename( old_name, name ) | |
return repository | |
def delete( self, repository ): | |
self._dao.delete( repository ) | |
FileSystemRepositoryService()._delete( repository ) | |
class FileSystemRepositoryService: | |
def _create( self, name, type_ = 'hg' ): | |
pass | |
def _fork( self, origin, fork, type_ = 'hg' ): | |
pass | |
def _rename( self, old_name, new_name ): | |
pass | |
def _delete( self, repository ): | |
pass | |
class UsersGroupRepositoryPermissionService( GenericService ): | |
def __init__( self ): | |
GenericService.__init__( self, UsersGroupRepositoryPermissionDao ) | |
def set_permission( self, repository, group, permission ): | |
current = self._dao.get_by_repository_and_group( repository, group ) | |
if current: | |
if permission is not current.permission: | |
current.permission = permission | |
self._dao.update( current ) | |
else: | |
current = UsersGroupRepoToPerm() | |
current.repository = repository | |
current.users_group = group | |
current.permission = permission | |
self._dao.save( current ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment