@@ -23,13 +23,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import traceback
import formencode
from operator import itemgetter
from formencode import htmlfill
from paste.httpexceptions import HTTPInternalServerError
from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect
from pylons.i18n.translation import _
@@ -89,13 +88,13 @@ class ReposController(BaseController):
' in order to rescan repositories') % repo_name,
category='error')
return redirect(url('repos'))
c.default_user_id = User.get_by_username('default').user_id
c.in_public_journal = self.sa.query(UserFollowing)\
c.in_public_journal = UserFollowing.query()\
.filter(UserFollowing.user_id == c.default_user_id)\
.filter(UserFollowing.follows_repository == c.repo_info).scalar()
if c.repo_info.stats:
last_rev = c.repo_info.stats.stat_on_revision
else:
@@ -107,36 +106,13 @@ class ReposController(BaseController):
if last_rev == 0 or c.repo_last_rev == 0:
c.stats_percentage = 0
c.stats_percentage = '%.2f' % ((float((last_rev)) /
c.repo_last_rev) * 100)
defaults = c.repo_info.get_dict()
group, repo_name = c.repo_info.groups_and_repo
defaults['repo_name'] = repo_name
defaults['repo_group'] = getattr(group[-1] if group else None,
'group_id', None)
#fill owner
if c.repo_info.user:
defaults.update({'user': c.repo_info.user.username})
replacement_user = self.sa.query(User)\
.filter(User.admin == True).first().username
defaults.update({'user': replacement_user})
#fill repository users
for p in c.repo_info.repo_to_perm:
defaults.update({'u_perm_%s' % p.user.username:
p.permission.permission_name})
#fill repository groups
for p in c.repo_info.users_group_to_perm:
defaults.update({'g_perm_%s' % p.users_group.users_group_name:
defaults = RepoModel()._get_defaults(repo_name)
return defaults
@HasPermissionAllDecorator('hg.admin')
def index(self, format='html'):
"""GET /repos: All items in the collection"""
# url('repos')
from rhodecode.controllers.api import JSONRPCController, JSONRPCError
from rhodecode.lib.auth import HasPermissionAllDecorator, HasPermissionAnyDecorator
from rhodecode.lib.auth import HasPermissionAllDecorator, \
HasPermissionAnyDecorator
from rhodecode.model.scm import ScmModel
from rhodecode.model.db import User, UsersGroup, Group, Repository
from rhodecode.model.repo import RepoModel
from rhodecode.model.user import UserModel
from rhodecode.model.repo_permission import RepositoryPermissionModel
@@ -294,14 +295,14 @@ class ApiController(JSONRPCController):
name = repository.repo_name,
type = repository.repo_type,
description = repository.description))
return result
@HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
def create_repo(self, apiuser, name, owner_name, description = '', repo_type = 'hg', \
private = False):
def create_repo(self, apiuser, name, owner_name, description='',
repo_type='hg', private=False):
"""
Create a repository
:param apiuser
:param name
:param description
@@ -361,12 +362,13 @@ class ApiController(JSONRPCController):
try:
user = User.get_by_username(user_name)
except NoResultFound:
raise JSONRPCError('unknown user %s' % user)
RepositoryPermissionModel().update_or_delete_user_permission(repo, user, perm)
RepositoryPermissionModel()\
.update_or_delete_user_permission(repo, user, perm)
except Exception:
log.error(traceback.format_exc())
raise JSONRPCError('failed to edit permission %(repo)s for %(user)s'
% dict(user = user_name, repo = repo_name))
@@ -39,23 +39,31 @@ from rhodecode.lib.auth import LoginRequ
HasRepoPermissionAnyDecorator, NotAnonymous
from rhodecode.lib.base import BaseRepoController, render
from rhodecode.lib.utils import invalidate_cache, action_logger
from rhodecode.model.forms import RepoSettingsForm, RepoForkForm
from rhodecode.model.db import User
from rhodecode.model.db import Group
log = logging.getLogger(__name__)
class SettingsController(BaseRepoController):
@LoginRequired()
def __before__(self):
super(SettingsController, self).__before__()
def __load_defaults(self):
c.repo_groups = Group.groups_choices()
c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups)
repo_model = RepoModel()
c.users_array = repo_model.get_users_js()
c.users_groups_array = repo_model.get_users_groups_js()
@HasRepoPermissionAllDecorator('repository.admin')
def index(self, repo_name):
c.repo_info = repo = repo_model.get_by_repo_name(repo_name)
if not repo:
h.flash(_('%s repository is not mapped to db perhaps'
@@ -63,55 +71,41 @@ class SettingsController(BaseRepoControl
' please run the application again'
return redirect(url('home'))
self.__load_defaults()
return htmlfill.render(
render('settings/repo_settings.html'),
defaults=defaults,
encoding="UTF-8",
force_defaults=False
)
def update(self, repo_name):
changed_name = repo_name
_form = RepoSettingsForm(edit=True,
old_data={'repo_name': repo_name})()
old_data={'repo_name': repo_name},
repo_groups=c.repo_groups_choices)()
form_result = _form.to_python(dict(request.POST))
repo_model.update(repo_name, form_result)
invalidate_cache('get_repo_cached_%s' % repo_name)
h.flash(_('Repository %s updated successfully' % repo_name),
category='success')
changed_name = form_result['repo_name']
changed_name = form_result['repo_name_full']
action_logger(self.rhodecode_user, 'user_updated_repo',
changed_name, '', self.sa)
except formencode.Invalid, errors:
c.repo_info = repo_model.get_by_repo_name(repo_name)
errors.value.update({'user': c.repo_info.user.username})
@@ -27,17 +27,14 @@ import os
import datetime
from datetime import date
from sqlalchemy import *
from sqlalchemy.exc import DatabaseError
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship, backref, joinedload, class_mapper, \
validates
from sqlalchemy.orm.interfaces import MapperExtension
from sqlalchemy.orm import relationship, joinedload, class_mapper, validates
from beaker.cache import cache_region, region_invalidate
from vcs import get_backend
from vcs.utils.helpers import get_scm
from vcs.exceptions import VCSError
from vcs.utils.lazy import LazyProperty
@@ -125,12 +122,13 @@ class BaseModel(object):
@classmethod
def query(cls):
return Session.query(cls)
def get(cls, id_):
if id_:
return cls.query().get(id_)
def getAll(cls):
return cls.query().all()
@@ -824,14 +822,13 @@ class Group(Base, BaseModel):
@property
def name(self):
return self.group_name.split(Group.url_sep())[-1]
def full_path(self):
return Group.url_sep().join([g.group_name for g in self.parents] +
[self.group_name])
return self.group_name
def full_path_splitted(self):
return self.group_name.split(Group.url_sep())
@@ -855,13 +852,14 @@ class Group(Base, BaseModel):
def get_new_name(self, group_name):
returns new full group name based on parent and new name
:param group_name:
path_prefix = self.parent_group.full_path_splitted if self.parent_group else []
path_prefix = (self.parent_group.full_path_splitted if
self.parent_group else [])
return Group.url_sep().join(path_prefix + [group_name])
class Permission(Base, BaseModel):
__tablename__ = 'permissions'
__table_args__ = {'extend_existing':True}
@@ -1065,6 +1063,7 @@ class CacheInvalidation(Base, BaseModel)
class DbMigrateVersion(Base, BaseModel):
__tablename__ = 'db_migrate_version'
repository_id = Column('repository_id', String(250), primary_key = True)
repository_path = Column('repository_path', Text)
version = Column('version', Integer)
@@ -193,14 +193,14 @@ class ValidPasswordsMatch(formencode.val
class ValidAuth(formencode.validators.FancyValidator):
messages = {
'invalid_password':_('invalid password'),
'invalid_login':_('invalid user name'),
'disabled_account':_('Your account is disabled')
}
#error mapping
e_dict = {'username':messages['invalid_login'],
'password':messages['invalid_password']}
e_dict_disable = {'username':messages['disabled_account']}
def validate_python(self, value, state):
@@ -250,12 +250,13 @@ def ValidRepoName(edit, old_data):
gr = Group.get(value.get('repo_group'))
group_path = gr.full_path
# value needs to be aware of group name in order to check
# db key This is an actual just the name to store in the
# database
repo_name_full = group_path + Group.url_sep() + repo_name
group_path = ''
repo_name_full = repo_name
value['repo_name_full'] = repo_name_full
@@ -613,22 +614,25 @@ def RepoForkForm(edit=False, old_data={}
repo_type = All(ValidForkType(old_data), OneOf(supported_backends))
chained_validators = [ValidForkName()]
return _RepoForkForm
def RepoSettingsForm(edit=False, old_data={}):
def RepoSettingsForm(edit=False, old_data={}, supported_backends=BACKENDS.keys(),
repo_groups=[]):
class _RepoForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = False
repo_name = All(UnicodeString(strip=True, min=1, not_empty=True),
SlugifyName())
description = UnicodeString(strip=True, min=1, not_empty=True)
repo_group = OneOf(repo_groups, hideList=True)
private = StringBoolean(if_missing=False)
chained_validators = [ValidRepoName(edit, old_data), ValidPerms, ValidSettings]
chained_validators = [ValidRepoName(edit, old_data), ValidPerms,
ValidSettings]
return _RepoForm
def ApplicationSettingsForm():
class _ApplicationSettingsForm(formencode.Schema):
@@ -91,12 +91,52 @@ class RepoModel(BaseModel):
users_groups_array = '[%s]' % '\n'.join([g_tmpl % \
(gr.users_group_id, gr.users_group_name,
len(gr.members))
for gr in users_groups])
return users_groups_array
def _get_defaults(self, repo_name):
Get's information about repository, and returns a dict for
usage in forms
:param repo_name:
repo_info = Repository.get_by_repo_name(repo_name)
if repo_info is None:
return None
defaults = repo_info.get_dict()
group, repo_name = repo_info.groups_and_repo
# fill owner
if repo_info.user:
defaults.update({'user': repo_info.user.username})
replacement_user = User.query().filter(User.admin ==
True).first().username
# fill repository users
for p in repo_info.repo_to_perm:
# fill repository groups
for p in repo_info.users_group_to_perm:
def update(self, repo_name, form_data):
cur_repo = self.get_by_repo_name(repo_name, cache=False)
# update permissions
for member, perm, member_type in form_data['perms_updates']:
@@ -148,13 +188,13 @@ class RepoModel(BaseModel):
for k, v in form_data.items():
if k == 'user':
cur_repo.user = User.get_by_username(v)
elif k == 'repo_name':
pass
elif k == 'repo_group':
cur_repo.group_id = v
cur_repo.group = Group.get(v)
setattr(cur_repo, k, v)
new_name = cur_repo.get_new_name(form_data['repo_name'])
cur_repo.repo_name = new_name
@@ -365,6 +405,7 @@ class RepoModel(BaseModel):
os.path.join(rm_path, 'rm__.%s' % alias))
#disable repo
shutil.move(rm_path, os.path.join(self.repos_path, 'rm__%s__%s' \
% (datetime.today()\
.strftime('%Y%m%d_%H%M%S_%f'),
repo.repo_name)))
@@ -47,58 +47,44 @@ class ReposGroupModel(BaseModel):
Get's the repositories root path from database
q = RhodeCodeUi.get_by_key('/').one()
return q.ui_value
def __create_group(self, group_name, parent_id):
def __create_group(self, group_name):
makes repositories group on filesystem
:param parent_id:
if parent_id:
paths = Group.get(parent_id).full_path.split(Group.url_sep())
parent_path = os.sep.join(paths)
parent_path = ''
create_path = os.path.join(self.repos_path, parent_path, group_name)
create_path = os.path.join(self.repos_path, group_name)
log.debug('creating new group in %s', create_path)
if os.path.isdir(create_path):
raise Exception('That directory already exists !')
os.makedirs(create_path)
def __rename_group(self, old, old_parent_id, new, new_parent_id):
def __rename_group(self, old, new):
Renames a group on filesystem
if old == new:
log.debug('skipping group rename')
return
log.debug('renaming repos group from %s to %s', old, new)
if new_parent_id:
paths = Group.get(new_parent_id).full_path.split(Group.url_sep())
new_parent_path = os.sep.join(paths)
new_parent_path = ''
if old_parent_id:
paths = Group.get(old_parent_id).full_path.split(Group.url_sep())
old_parent_path = os.sep.join(paths)
old_parent_path = ''
old_path = os.path.join(self.repos_path, old_parent_path, old)
new_path = os.path.join(self.repos_path, new_parent_path, new)
old_path = os.path.join(self.repos_path, old)
new_path = os.path.join(self.repos_path, new)
log.debug('renaming repos paths from %s to %s', old_path, new_path)
if os.path.isdir(new_path):
raise Exception('Was trying to rename to already '
'existing dir %s' % new_path)
@@ -111,55 +97,59 @@ class ReposGroupModel(BaseModel):
:param group: instance of group from database
paths = group.full_path.split(Group.url_sep())
paths = os.sep.join(paths)
rm_path = os.path.join(self.repos_path, paths)
if os.path.isdir(rm_path):
# delete only if that path really exists
os.rmdir(rm_path)
def create(self, form_data):
new_repos_group = Group()
new_repos_group.group_name = form_data['group_name']
new_repos_group.group_description = \
form_data['group_description']
new_repos_group.group_parent_id = form_data['group_parent_id']
new_repos_group.group_description = form_data['group_description']
new_repos_group.parent_group = Group.get(form_data['group_parent_id'])
new_repos_group.group_name = new_repos_group.get_new_name(form_data['group_name'])
self.sa.add(new_repos_group)
self.__create_group(form_data['group_name'],
form_data['group_parent_id'])
self.__create_group(new_repos_group.group_name)
self.sa.commit()
return new_repos_group
except:
self.sa.rollback()
raise
def update(self, repos_group_id, form_data):
repos_group = Group.get(repos_group_id)
old_name = repos_group.group_name
old_parent_id = repos_group.group_parent_id
old_path = repos_group.full_path
repos_group.group_name = form_data['group_name']
repos_group.group_description = \
repos_group.group_parent_id = form_data['group_parent_id']
# change properties
repos_group.group_description = form_data['group_description']
repos_group.parent_group = Group.get(form_data['group_parent_id'])
repos_group.group_name = repos_group.get_new_name(form_data['group_name'])
new_path = repos_group.full_path
self.sa.add(repos_group)
if old_name != form_data['group_name'] or (old_parent_id !=
form_data['group_parent_id']):
self.__rename_group(old = old_name, old_parent_id = old_parent_id,
new = form_data['group_name'],
new_parent_id = form_data['group_parent_id'])
self.__rename_group(old_path, new_path)
# we need to get all repositories from this new group and
# rename them accordingly to new group path
for r in repos_group.repositories:
r.repo_name = r.get_new_name(r.just_name)
self.sa.add(r)
return repos_group
def delete(self, users_group_id):
@@ -93,12 +93,13 @@ class UserModel(BaseModel):
def create_ldap(self, username, password, user_dn, attrs):
Checks if user is in database, if not creates this user marked
as ldap user
:param username:
:param password:
:param user_dn:
:param attrs:
from rhodecode.lib.auth import get_crypt_password
@@ -383,6 +384,7 @@ class UserModel(BaseModel):
# given from other sources
if PERM_WEIGHTS[p] > PERM_WEIGHTS[cur_perm]:
user.permissions['repositories'][perm.UsersGroupRepoToPerm.
repository.repo_name] = p
return user
@@ -31,13 +31,20 @@
<label for="repo_name">${_('Name')}:</label>
</div>
<div class="input input-medium">
${h.text('repo_name',class_="small")}
<div class="field">
<div class="label">
<label for="repo_group">${_('Repository group')}:</label>
<div class="input">
${h.select('repo_group','',c.repo_groups,class_="medium")}
<div class="label label-textarea">
<label for="description">${_('Description')}:</label>
<div class="textarea text-area editor">
${h.textarea('description',cols=23,rows=5)}
@@ -74,12 +74,10 @@ class TestController(TestCase):
self.fail('could not login using %s %s' % (username, password))
self.assertEqual(response.status, '302 Found')
self.assertEqual(response.session['rhodecode_user'].username, username)
return response.follow()
def checkSessionFlash(self, response, msg):
self.assertTrue('flash' in response.session)
self.assertTrue(msg in response.session['flash'][0][1])
Status change: