Fix permissions when converting a user to an org

Fixes #1366
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):
# Change the user to an organization.
user_obj.organization = True
if user_obj.robot:
raise DataModelException('Cannot convert a robot into an organization')
# disable this account for login.
user_obj.password_hash = None
user_obj.save()
with db_transaction():
# Change the user to an organization and disable this account for login.
user_obj.organization = True
user_obj.password_hash = None
user_obj.save()
# Clear any federated auth pointing to this user
FederatedLogin.delete().where(FederatedLogin.user == user_obj).execute()
# Clear any federated auth pointing to this user.
FederatedLogin.delete().where(FederatedLogin.user == user_obj).execute()
# Create a team for the owners
owners_team = team.create_team('owners', user_obj, 'admin')
# Delete any user-specific permissions on repositories.
(RepositoryPermission.delete()
.where(RepositoryPermission.user == user_obj)
.execute())
# Add the user who will admin the org to the owners team
team.add_user_to_team(admin_user, owners_team)
# Create a team for the owners
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):