Search in sources :

Example 1 with ProjectRole

use of org.apache.cloudstack.acl.ProjectRole in project cloudstack by apache.

the class UpdateProjectRoleCmd method execute.

// ///////////////////////////////////////////////////
// ////////////// API Implementation /////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() {
    ProjectRole role = projRoleService.findProjectRole(getId(), getProjectId());
    if (role == null) {
        throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Invalid project role id provided");
    }
    role = projRoleService.updateProjectRole(role, getProjectId(), getProjectRoleName(), getProjectRoleDescription());
    setupProjectRoleResponse(role);
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) ProjectRole(org.apache.cloudstack.acl.ProjectRole)

Example 2 with ProjectRole

use of org.apache.cloudstack.acl.ProjectRole in project cloudstack by apache.

the class CreateProjectRolePermissionCmd method execute.

// ///////////////////////////////////////////////////
// ///////////// API Implementation///////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() {
    ProjectRole projectRole = projRoleService.findProjectRole(getProjectRoleId(), getProjectId());
    if (projectRole == null) {
        throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Invalid project role ID provided");
    }
    CallContext.current().setEventDetails("Project Role ID: " + projectRole.getId() + ", Rule:" + getRule() + ", Permission: " + getPermission() + ", Description: " + getDescription());
    final ProjectRolePermission projectRolePermission = projRoleService.createProjectRolePermission(this);
    if (projectRolePermission == null) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create project role permission");
    }
    setupResponse(projectRolePermission, projectRole);
}
Also used : ProjectRolePermission(org.apache.cloudstack.acl.ProjectRolePermission) ServerApiException(org.apache.cloudstack.api.ServerApiException) ProjectRole(org.apache.cloudstack.acl.ProjectRole)

Example 3 with ProjectRole

use of org.apache.cloudstack.acl.ProjectRole in project cloudstack by apache.

the class ProjectManagerImpl method addAccountToProject.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_ACCOUNT_ADD, eventDescription = "adding account to project", async = true)
public boolean addAccountToProject(long projectId, String accountName, String email, Long projectRoleId, Role projectRoleType) {
    Account caller = CallContext.current().getCallingAccount();
    // check that the project exists
    Project project = getProject(projectId);
    if (project == null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified id");
        ex.addProxyObject(String.valueOf(projectId), "projectId");
        throw ex;
    }
    // User can be added to Active project only
    if (project.getState() != Project.State.Active) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Can't add account to the specified project id in state=" + project.getState() + " as it's no longer active");
        ex.addProxyObject(project.getUuid(), "projectId");
        throw ex;
    }
    // check that account-to-add exists
    Account account = null;
    if (accountName != null) {
        account = _accountMgr.getActiveAccountByName(accountName, project.getDomainId());
        if (account == null) {
            InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find account name=" + accountName + " in specified domain id");
            DomainVO domain = ApiDBUtils.findDomainById(project.getDomainId());
            String domainUuid = String.valueOf(project.getDomainId());
            if (domain != null) {
                domainUuid = domain.getUuid();
            }
            ex.addProxyObject(domainUuid, "domainId");
            throw ex;
        }
        CallContext.current().setProject(project);
        // verify permissions - only project owner can assign
        _accountMgr.checkAccess(caller, AccessType.ModifyProject, true, _accountMgr.getAccount(project.getProjectAccountId()));
        // Check if the account already added to the project
        ProjectAccount projectAccount = _projectAccountDao.findByProjectIdAccountId(projectId, account.getId());
        if (projectAccount != null) {
            s_logger.debug("Account " + accountName + " already added to the project id=" + projectId);
            return true;
        }
    }
    if (projectRoleId != null && projectRoleId < 1L) {
        throw new InvalidParameterValueException("Invalid project role id provided");
    }
    ProjectRole projectRole = null;
    if (projectRoleId != null) {
        projectRole = projectRoleDao.findById(projectRoleId);
        if (projectRole == null || projectRole.getProjectId() != projectId) {
            throw new InvalidParameterValueException("Invalid project role ID for the given project");
        }
    }
    if (_invitationRequired) {
        return inviteAccountToProject(project, account, email, projectRoleType, projectRole);
    } else {
        if (account == null) {
            throw new InvalidParameterValueException("Account information is required for assigning account to the project");
        }
        if (assignAccountToProject(project, account.getId(), projectRoleType, null, Optional.ofNullable(projectRole).map(ProjectRole::getId).orElse(null)) != null) {
            return true;
        } else {
            s_logger.warn("Failed to add account " + accountName + " to project id=" + projectId);
            return false;
        }
    }
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ProjectRole(org.apache.cloudstack.acl.ProjectRole) ActionEvent(com.cloud.event.ActionEvent)

Example 4 with ProjectRole

use of org.apache.cloudstack.acl.ProjectRole in project cloudstack by apache.

the class ProjectManagerImpl method addUserToProject.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_USER_ADD, eventDescription = "adding user to project", async = true)
public boolean addUserToProject(Long projectId, String username, String email, Long projectRoleId, Role projectRole) {
    Account caller = CallContext.current().getCallingAccount();
    Project project = getProject(projectId);
    if (project == null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified id");
        ex.addProxyObject(String.valueOf(projectId), "projectId");
        throw ex;
    }
    if (project.getState() != State.Active) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Can't add user to the specified project id in state=" + project.getState() + " as it isn't currently active");
        ex.addProxyObject(project.getUuid(), "projectId");
        throw ex;
    }
    User user = userDao.getUserByName(username, project.getDomainId());
    if (user == null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Invalid user ID provided");
        ex.addProxyObject(String.valueOf(username), "userId");
        throw ex;
    }
    CallContext.current().setProject(project);
    _accountMgr.checkAccess(caller, AccessType.ModifyProject, true, _accountMgr.getAccount(project.getProjectAccountId()));
    Account userAccount = _accountDao.findById(user.getAccountId());
    if (_projectAccountDao.findByProjectIdAccountId(projectId, userAccount.getAccountId()) != null) {
        throw new InvalidParameterValueException("User belongs to account " + userAccount.getAccountId() + " which is already part of the project");
    }
    ProjectAccount projectAccountUser = _projectAccountDao.findByProjectIdUserId(projectId, user.getAccountId(), user.getId());
    if (projectAccountUser != null) {
        s_logger.info("User with id: " + user.getId() + " is already added to the project with id: " + projectId);
        return true;
    }
    if (projectRoleId != null && projectRoleId < 1L) {
        throw new InvalidParameterValueException("Invalid project role id provided");
    }
    ProjectRole role = null;
    if (projectRoleId != null) {
        role = projectRoleDao.findById(projectRoleId);
        if (role == null || !role.getProjectId().equals(projectId)) {
            throw new InvalidParameterValueException("Invalid project role ID for the given project");
        }
    }
    if (_invitationRequired) {
        return inviteUserToProject(project, user, email, projectRole, role);
    } else {
        if (username == null) {
            throw new InvalidParameterValueException("User information (ID) is required to add user to the project");
        }
        if (assignUserToProject(project, user.getId(), user.getAccountId(), projectRole, Optional.ofNullable(role).map(ProjectRole::getId).orElse(null)) != null) {
            return true;
        }
        s_logger.warn("Failed to add user to project with id: " + projectId);
        return false;
    }
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ProjectRole(org.apache.cloudstack.acl.ProjectRole) ActionEvent(com.cloud.event.ActionEvent)

Example 5 with ProjectRole

use of org.apache.cloudstack.acl.ProjectRole in project cloudstack by apache.

the class DeleteProjectRoleCmd method execute.

// ///////////////////////////////////////////////////
// ///////////// API Implementation///////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() {
    ProjectRole role = projRoleService.findProjectRole(getId(), getProjectId());
    if (role == null) {
        throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Cannot find project role with provided id");
    }
    CallContext.current().setEventDetails("Deleting Project Role with id: " + role.getId());
    boolean result = projRoleService.deleteProjectRole(role, getProjectId());
    SuccessResponse response = new SuccessResponse(getCommandName());
    response.setSuccess(result);
    setResponseObject(response);
}
Also used : SuccessResponse(org.apache.cloudstack.api.response.SuccessResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ProjectRole(org.apache.cloudstack.acl.ProjectRole)

Aggregations

ProjectRole (org.apache.cloudstack.acl.ProjectRole)9 ServerApiException (org.apache.cloudstack.api.ServerApiException)5 ProjectRolePermission (org.apache.cloudstack.acl.ProjectRolePermission)3 ActionEvent (com.cloud.event.ActionEvent)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 Account (com.cloud.user.Account)2 ArrayList (java.util.ArrayList)2 ListResponse (org.apache.cloudstack.api.response.ListResponse)2 SuccessResponse (org.apache.cloudstack.api.response.SuccessResponse)2 DomainVO (com.cloud.domain.DomainVO)1 User (com.cloud.user.User)1 ProjectRolePermissionResponse (org.apache.cloudstack.api.response.ProjectRolePermissionResponse)1 ProjectRoleResponse (org.apache.cloudstack.api.response.ProjectRoleResponse)1