2017-07-25 17:41:20 +00:00
|
|
|
from app import avatar
|
|
|
|
from data import model
|
2018-03-09 20:55:51 +00:00
|
|
|
from data.database import User, FederatedLogin, Team as TeamTable, Repository, RobotAccountMetadata
|
2017-08-10 00:33:14 +00:00
|
|
|
from endpoints.api.robot_models_interface import (RobotInterface, Robot, RobotWithPermissions, Team,
|
|
|
|
Permission)
|
2017-07-25 17:41:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RobotPreOCIModel(RobotInterface):
|
|
|
|
def list_robot_permissions(self, username):
|
|
|
|
permissions = model.permission.list_robot_permissions(username)
|
|
|
|
return [Permission(permission.repository.name, permission.repository.visibility.name, permission.role.name) for
|
|
|
|
permission in permissions]
|
|
|
|
|
|
|
|
def list_entity_robot_permission_teams(self, prefix, include_permissions=False):
|
|
|
|
tuples = model.user.list_entity_robot_permission_teams(prefix,
|
|
|
|
include_permissions=include_permissions)
|
|
|
|
robots = {}
|
|
|
|
robot_teams = set()
|
|
|
|
|
|
|
|
for robot_tuple in tuples:
|
|
|
|
robot_name = robot_tuple.get(User.username)
|
|
|
|
if robot_name not in robots:
|
|
|
|
robot_dict = {
|
|
|
|
'name': robot_name,
|
|
|
|
'token': robot_tuple.get(FederatedLogin.service_ident),
|
2018-03-09 18:55:19 +00:00
|
|
|
'created': robot_tuple.get(User.creation_date),
|
2018-03-09 20:55:51 +00:00
|
|
|
'description': robot_tuple.get(RobotAccountMetadata.description),
|
|
|
|
'unstructured_metadata': robot_tuple.get(RobotAccountMetadata.unstructured_json),
|
2017-07-25 17:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if include_permissions:
|
|
|
|
robot_dict.update({
|
|
|
|
'teams': [],
|
2018-03-09 20:55:51 +00:00
|
|
|
'repositories': [],
|
2017-07-25 17:41:20 +00:00
|
|
|
})
|
2018-03-09 20:55:51 +00:00
|
|
|
|
|
|
|
robots[robot_name] = Robot(robot_dict['name'], robot_dict['token'], robot_dict['created'],
|
|
|
|
robot_dict['description'], robot_dict['unstructured_metadata'])
|
2017-07-25 17:41:20 +00:00
|
|
|
if include_permissions:
|
2017-08-10 00:33:14 +00:00
|
|
|
team_name = robot_tuple.get(TeamTable.name)
|
2017-07-25 17:41:20 +00:00
|
|
|
repository_name = robot_tuple.get(Repository.name)
|
|
|
|
|
|
|
|
if team_name is not None:
|
|
|
|
check_key = robot_name + ':' + team_name
|
|
|
|
if check_key not in robot_teams:
|
|
|
|
robot_teams.add(check_key)
|
|
|
|
|
|
|
|
robot_dict['teams'].append(Team(
|
|
|
|
team_name,
|
|
|
|
avatar.get_data(team_name, team_name, 'team')
|
|
|
|
))
|
|
|
|
|
|
|
|
if repository_name is not None:
|
|
|
|
if repository_name not in robot_dict['repositories']:
|
|
|
|
robot_dict['repositories'].append(repository_name)
|
2018-03-09 18:55:19 +00:00
|
|
|
robots[robot_name] = RobotWithPermissions(robot_dict['name'], robot_dict['token'],
|
|
|
|
robot_dict['created'], robot_dict['teams'],
|
2018-03-09 20:55:51 +00:00
|
|
|
robot_dict['repositories'],
|
|
|
|
robot_dict['description'])
|
2017-07-25 17:41:20 +00:00
|
|
|
|
|
|
|
return robots.values()
|
|
|
|
|
|
|
|
def regenerate_user_robot_token(self, robot_shortname, owning_user):
|
2018-03-09 20:55:51 +00:00
|
|
|
robot, password, metadata = model.user.regenerate_robot_token(robot_shortname, owning_user)
|
|
|
|
return Robot(robot.username, password, robot.creation_date, metadata.description,
|
|
|
|
metadata.unstructured_json)
|
2017-07-25 17:41:20 +00:00
|
|
|
|
|
|
|
def regenerate_org_robot_token(self, robot_shortname, orgname):
|
|
|
|
parent = model.organization.get_organization(orgname)
|
2018-03-09 20:55:51 +00:00
|
|
|
robot, password, metadata = model.user.regenerate_robot_token(robot_shortname, parent)
|
|
|
|
return Robot(robot.username, password, robot.creation_date, metadata.description,
|
|
|
|
metadata.unstructured_json)
|
2017-07-25 17:41:20 +00:00
|
|
|
|
|
|
|
def delete_robot(self, robot_username):
|
|
|
|
model.user.delete_robot(robot_username)
|
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
def create_user_robot(self, robot_shortname, owning_user, description, unstructured_metadata):
|
|
|
|
robot, password = model.user.create_robot(robot_shortname, owning_user, description or '',
|
|
|
|
unstructured_metadata)
|
|
|
|
return Robot(robot.username, password, robot.creation_date, description or '',
|
|
|
|
unstructured_metadata)
|
2017-07-25 17:41:20 +00:00
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
def create_org_robot(self, robot_shortname, orgname, description, unstructured_metadata):
|
2017-07-25 17:41:20 +00:00
|
|
|
parent = model.organization.get_organization(orgname)
|
2018-03-09 20:55:51 +00:00
|
|
|
robot, password = model.user.create_robot(robot_shortname, parent, description or '',
|
|
|
|
unstructured_metadata)
|
|
|
|
return Robot(robot.username, password, robot.creation_date, description or '',
|
|
|
|
unstructured_metadata)
|
2017-07-25 17:41:20 +00:00
|
|
|
|
|
|
|
def get_org_robot(self, robot_shortname, orgname):
|
|
|
|
parent = model.organization.get_organization(orgname)
|
2018-03-09 20:55:51 +00:00
|
|
|
robot, password, metadata = model.user.get_robot_and_metadata(robot_shortname, parent)
|
|
|
|
return Robot(robot.username, password, robot.creation_date, metadata.description,
|
|
|
|
metadata.unstructured_json)
|
2017-07-25 17:41:20 +00:00
|
|
|
|
|
|
|
def get_user_robot(self, robot_shortname, owning_user):
|
2018-03-09 20:55:51 +00:00
|
|
|
robot, password, metadata = model.user.get_robot_and_metadata(robot_shortname, owning_user)
|
|
|
|
return Robot(robot.username, password, robot.creation_date, metadata.description,
|
|
|
|
metadata.unstructured_json)
|
2017-07-25 17:41:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
pre_oci_model = RobotPreOCIModel()
|