Fix permissions when converting a user to an org

Fixes 
This commit is contained in:
Joseph Schorr 2016-04-14 17:39:45 -04:00
parent a65012a71e
commit c604dbd0f6
3 changed files with 45 additions and 20 deletions

View file

@ -35,23 +35,30 @@ def get_organization(name):
def convert_user_to_organization(user_obj, admin_user): def convert_user_to_organization(user_obj, admin_user):
# Change the user to an organization. if user_obj.robot:
user_obj.organization = True raise DataModelException('Cannot convert a robot into an organization')
# disable this account for login. with db_transaction():
user_obj.password_hash = None # Change the user to an organization and disable this account for login.
user_obj.save() user_obj.organization = True
user_obj.password_hash = None
user_obj.save()
# Clear any federated auth pointing to this user # Clear any federated auth pointing to this user.
FederatedLogin.delete().where(FederatedLogin.user == user_obj).execute() FederatedLogin.delete().where(FederatedLogin.user == user_obj).execute()
# Create a team for the owners # Delete any user-specific permissions on repositories.
owners_team = team.create_team('owners', user_obj, 'admin') (RepositoryPermission.delete()
.where(RepositoryPermission.user == user_obj)
.execute())
# Add the user who will admin the org to the owners team # Create a team for the owners
team.add_user_to_team(admin_user, owners_team) owners_team = team.create_team('owners', user_obj, 'admin')
return user_obj # Add the user who will admin the org to the owners team
team.add_user_to_team(admin_user, owners_team)
return user_obj
def get_user_organizations(username): def get_user_organizations(username):

View file

@ -1,15 +1,19 @@
<div class="convert-user-to-org-element"> <div class="convert-user-to-org-element">
<!-- Step 0 --> <!-- Step 0 -->
<div class="panel" ng-show="convertStep == 0"> <div ng-show="convertStep == 0">
<div class="panel-body" ng-show="user.organizations.length > 0"> <div ng-show="user.organizations.length > 0">
<div class="co-alert co-alert-info"> Cannot convert this account into an organization, as it is a member of {{user.organizations.length}} other
Cannot convert this account into an organization, as it is a member of {{user.organizations.length}} other organization{{user.organizations.length > 1 ? 's' : ''}}. Please leave
organization{{user.organizations.length > 1 ? 's' : ''}}. Please leave {{user.organizations.length > 1 ? 'those organizations' : 'that organization'}} first.
{{user.organizations.length > 1 ? 'those organizations' : 'that organization'}} first.
</div> <ul>
<li ng-repeat="org in user.organizations">
{{ org.name }}
</li>
</ul>
</div> </div>
<div class="panel-body" ng-show="user.organizations.length == 0"> <div ng-show="user.organizations.length == 0">
<div class="co-alert co-alert-warning"> <div class="co-alert co-alert-warning">
Note: Converting a user account into an organization <b>cannot be undone</b> Note: Converting a user account into an organization <b>cannot be undone</b>
</div> </div>

View file

@ -417,6 +417,15 @@ class TestConvertToOrganization(ApiTestCase):
def test_convert(self): def test_convert(self):
self.login(READ_ACCESS_USER) self.login(READ_ACCESS_USER)
# Add at least one permission for the read-user.
read_user = model.user.get_user(READ_ACCESS_USER)
simple_repo = model.repository.get_repository(ADMIN_ACCESS_USER, 'simple')
read_role = database.Role.get(name='read')
database.RepositoryPermission.create(user=read_user, repository=simple_repo, role=read_role)
# Convert the read user into an organization.
json = self.postJsonResponse(ConvertToOrganization, json = self.postJsonResponse(ConvertToOrganization,
data={'adminUser': ADMIN_ACCESS_USER, data={'adminUser': ADMIN_ACCESS_USER,
'adminPassword': 'password', 'adminPassword': 'password',
@ -436,6 +445,11 @@ class TestConvertToOrganization(ApiTestCase):
self.assertEquals(READ_ACCESS_USER, json['name']) self.assertEquals(READ_ACCESS_USER, json['name'])
self.assertEquals(True, json['is_admin']) self.assertEquals(True, json['is_admin'])
# Verify the now-org has no permissions.
count = (database.RepositoryPermission.select()
.where(database.RepositoryPermission.user == organization)
.count())
self.assertEquals(0, count)
def test_convert_via_email(self): def test_convert_via_email(self):
self.login(READ_ACCESS_USER) self.login(READ_ACCESS_USER)