##// END OF EJS Templates
Merge with upstream
Merge with upstream

File last commit:

r1587:8898a79a beta
r1588:8ac66733 merge beta
Show More
api.py
353 lines | 11.9 KiB | text/x-python | PythonLexer
Extended API...
r1500 import traceback
import logging
Beginning of API implementation for rhodecode
r1445 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 from rhodecode.lib.auth import HasPermissionAllDecorator, HasPermissionAnyDecorator
Beginning of API implementation for rhodecode
r1445 from rhodecode.model.scm import ScmModel
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 from rhodecode.model.db import User, UsersGroup, Group, Repository
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 from rhodecode.model.repo import RepoModel
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 from rhodecode.model.user import UserModel
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 from rhodecode.model.repo_permission import RepositoryPermissionModel
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 from rhodecode.model.users_group import UsersGroupModel
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 from rhodecode.model import users_group
Extended API...
r1500
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 log = logging.getLogger( __name__ )
Extended API...
r1500
Beginning of API implementation for rhodecode
r1445
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 class ApiController( JSONRPCController ):
Beginning of API implementation for rhodecode
r1445 """
API Controller
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Beginning of API implementation for rhodecode
r1445 Each method needs to have USER as argument this is then based on given
API_KEY propagated as instance of user object
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Beginning of API implementation for rhodecode
r1445 Preferably this should be first argument also
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Each function should also **raise** JSONRPCError for any
Beginning of API implementation for rhodecode
r1445 errors that happens
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Beginning of API implementation for rhodecode
r1445 """
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 @HasPermissionAllDecorator( 'hg.admin' )
def pull( self, apiuser, repo ):
Beginning of API implementation for rhodecode
r1445 """
Dispatch pull action on given repo
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Extended API...
r1500 :param user:
:param repo:
Beginning of API implementation for rhodecode
r1445 """
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 if Repository.is_valid( repo ) is False:
raise JSONRPCError( 'Unknown repo "%s"' % repo )
Beginning of API implementation for rhodecode
r1445 try:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 ScmModel().pull_changes( repo, self.rhodecode_user.username )
Beginning of API implementation for rhodecode
r1445 return 'Pulled from %s' % repo
except Exception:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 raise JSONRPCError( 'Unable to pull changes from "%s"' % repo )
Beginning of API implementation for rhodecode
r1445
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 @HasPermissionAllDecorator( 'hg.admin' )
def get_user( self, apiuser, username ):
""""
Get a user by username
:param apiuser
:param username
"""
Beginning of API implementation for rhodecode
r1445
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 user = User.by_username( username )
if not user:
return None
return dict( id = user.user_id,
username = user.username,
firstname = user.name,
lastname = user.lastname,
email = user.email,
active = user.active,
admin = user.admin,
ldap = user.ldap_dn )
@HasPermissionAllDecorator( 'hg.admin' )
def get_users( self, apiuser ):
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 """"
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 Get all users
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
:param apiuser
"""
result = []
for user in User.getAll():
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 result.append( dict( id = user.user_id,
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 username = user.username,
firstname = user.name,
lastname = user.lastname,
email = user.email,
active = user.active,
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 admin = user.admin,
ldap = user.ldap_dn ) )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 return result
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 @HasPermissionAllDecorator( 'hg.admin' )
def create_user( self, apiuser, username, password, firstname,
lastname, email, active = True, admin = False, ldap_dn = None ):
Extended API...
r1500 """
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 Create new user
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Extended API...
r1500 :param apiuser:
:param username:
:param password:
:param name:
:param lastname:
:param email:
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 :param active:
:param admin:
:param ldap_dn:
Extended API...
r1500 """
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Extended API...
r1500 try:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 form_data = dict( username = username,
password = password,
active = active,
admin = admin,
name = firstname,
lastname = lastname,
email = email,
ldap_dn = ldap_dn )
UserModel().create_ldap( username, password, ldap_dn, form_data )
return dict( msg = 'created new user %s' % username )
Extended API...
r1500 except Exception:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 log.error( traceback.format_exc() )
raise JSONRPCError( 'failed to create user %s' % username )
@HasPermissionAllDecorator( 'hg.admin' )
def get_users_group( self, apiuser, group_name ):
""""
Get users group by name
:param apiuser
:param group_name
"""
users_group = UsersGroup.get_by_group_name( group_name )
if not users_group:
return None
Beginning of API implementation for rhodecode
r1445
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 members = []
for user in users_group.members:
user = user.user
members.append( dict( id = user.user_id,
username = user.username,
firstname = user.name,
lastname = user.lastname,
email = user.email,
active = user.active,
admin = user.admin,
ldap = user.ldap_dn ) )
return dict( id = users_group.users_group_id,
name = users_group.users_group_name,
active = users_group.users_group_active,
members = members )
@HasPermissionAllDecorator( 'hg.admin' )
def get_users_groups( self, apiuser ):
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 """"
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 Get all users groups
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
:param apiuser
"""
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 result = []
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 for users_group in UsersGroup.getAll():
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 members = []
for user in users_group.members:
user = user.user
members.append( dict( id = user.user_id,
username = user.username,
firstname = user.name,
lastname = user.lastname,
email = user.email,
active = user.active,
admin = user.admin,
ldap = user.ldap_dn ) )
result.append( dict( id = users_group.users_group_id,
name = users_group.users_group_name,
active = users_group.users_group_active,
members = members ) )
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 return result
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 @HasPermissionAllDecorator( 'hg.admin' )
def create_users_group( self, apiuser, name, active = True ):
Extended API...
r1500 """
Creates an new usergroup
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Extended API...
r1500 :param name:
:param active:
"""
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
Extended API...
r1500 try:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 form_data = dict( users_group_name = name,
users_group_active = active )
ug = UsersGroup.create( form_data )
return dict( id = ug.users_group_id,
msg = 'created new users group %s' % name )
Extended API...
r1500 except Exception:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 log.error( traceback.format_exc() )
raise JSONRPCError( 'failed to create group %s' % name )
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 @HasPermissionAllDecorator( 'hg.admin' )
def add_user_to_users_group( self, apiuser, group_name, user_name ):
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 """"
Add a user to a group
:param apiuser
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 :param group_name
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 :param user_name
"""
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 try:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 users_group = UsersGroup.get_by_group_name( group_name )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 if not users_group:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 raise JSONRPCError( 'unknown users group %s' % group_name )
user = User.by_username( user_name )
if not user:
raise JSONRPCError( 'unknown user %s' % user_name )
ugm = UsersGroupModel().add_user_to_group( users_group, user )
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 return dict( id = ugm.users_group_member_id,
msg = 'created new users group member' )
except Exception:
log.error( traceback.format_exc() )
raise JSONRPCError( 'failed to create users group member' )
@HasPermissionAnyDecorator( 'hg.admin' )
def get_repo( self, apiuser, repo_name ):
""""
Get repository by name
:param apiuser
:param repo_name
"""
repo = Repository.by_repo_name( repo_name )
if not repo:
return None
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 members = []
for user in repo.repo_to_perm:
perm = user.permission.permission_name
user = user.user
members.append( dict( type_ = "user",
id = user.user_id,
username = user.username,
firstname = user.name,
lastname = user.lastname,
email = user.email,
active = user.active,
admin = user.admin,
ldap = user.ldap_dn,
permission = perm ) )
for users_group in repo.users_group_to_perm:
perm = users_group.permission.permission_name
users_group = users_group.users_group
members.append( dict( type_ = "users_group",
id = users_group.users_group_id,
name = users_group.users_group_name,
active = users_group.users_group_active,
permission = perm ) )
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 return dict( id = repo.repo_id,
name = repo.repo_name,
type = repo.repo_type,
description = repo.description,
members = members )
@HasPermissionAnyDecorator( 'hg.admin' )
def get_repos( self, apiuser ):
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 """"
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 Get all repositories
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
:param apiuser
"""
Nicolas VINOT
Implement all CRUD API operation for repo
r1587
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 result = []
for repository in Repository.getAll():
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 result.append( dict( id = repository.repo_id,
name = repository.repo_name,
type = repository.repo_type,
description = repository.description ) )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 return result
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 @HasPermissionAnyDecorator( 'hg.admin', 'hg.create.repository' )
def create_repo( self, apiuser, name, owner_name, description = None, repo_type = 'hg', \
private = False, group_name = None ):
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 """
Create a repository
:param apiuser
:param name
:param description
:param type
:param private
:param owner_name
:param group_name
:param clone
"""
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 try:
if group_name:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 group = Group.get_by_group_name( group_name )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 if group is None:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 raise JSONRPCError( 'unknown group %s' % group_name )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 else:
group = None
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 owner = User.by_username( owner_name )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 if owner is None:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 raise JSONRPCError( 'unknown user %s' % owner )
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 RepoModel().create( { "repo_name" : name,
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 "repo_name_full" : name,
"description" : description,
"private" : private,
"repo_type" : repo_type,
"repo_group" : group,
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 "clone_uri" : None }, owner )
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584 except Exception:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 log.error( traceback.format_exc() )
raise JSONRPCError( 'failed to create repository %s' % name )
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 @HasPermissionAnyDecorator( 'hg.admin' )
def add_user_to_repo( self, apiuser, repo_name, user_name, perm ):
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 """
Add permission for a user to a repository
Nicolas VINOT
Improve API with user/group/repo CRUD methods
r1584
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 :param apiuser
:param repo_name
:param user_name
:param perm
"""
try:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 repo = Repository.by_repo_name( repo_name )
if not repo:
raise JSONRPCError( 'unknown repository %s' % repo )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 user = User.by_username( user_name )
if not user:
raise JSONRPCError( 'unknown user %s' % user )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 RepositoryPermissionModel().updateOrDeleteUserPermission( repo, user, perm )
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 except Exception:
Nicolas VINOT
Implement all CRUD API operation for repo
r1587 log.error( traceback.format_exc() )
raise JSONRPCError( 'failed to edit permission %(repo)s for %(user)s'
Nicolas VINOT
Add API for repositories and groups (creation, permission)
r1586 % dict( user = user_name, repo = repo_name ) )