Пример #1
0
// Delete ...
func (pma *ProjectMemberAPI) Delete() {
	currentUserID := pma.currentUserID
	pid := pma.project.ProjectID
	if !hasProjectAdminRole(currentUserID, pid) {
		log.Warningf("Current user, id: %d does not have project admin role for project, id:", currentUserID, pid)
		pma.RenderError(http.StatusForbidden, "")
		return
	}

	mid := pma.memberID

	err := dao.DeleteProjectMember(pid, mid)
	if err != nil {
		log.Errorf("Failed to delete project roles for user, user id: %d, project id: %d, error: %v", mid, pid, err)
		pma.RenderError(http.StatusInternalServerError, "Failed to update data in DB")
		return
	}
}
Пример #2
0
// Put ...
func (pma *ProjectMemberAPI) Put() {
	currentUserID := pma.currentUserID
	pid := pma.project.ProjectID
	if !hasProjectAdminRole(currentUserID, pid) {
		log.Warningf("Current user, id: %d does not have project admin role for project, id:", currentUserID, pid)
		pma.RenderError(http.StatusForbidden, "")
		return
	}

	mid := pma.memberID

	var req memberReq
	pma.DecodeJSONReq(&req)
	roleList, err := dao.GetUserProjectRoles(mid, pid)
	if len(roleList) == 0 {
		log.Warningf("User is not in project, user id: %d, project id: %d", mid, pid)
		pma.RenderError(http.StatusNotFound, "user not exist in project")
		return
	}
	//TODO: delete and insert should in one transaction
	//delete user project role record for the given user
	err = dao.DeleteProjectMember(pid, mid)
	if err != nil {
		log.Errorf("Failed to delete project roles for user, user id: %d, project id: %d, error: %v", mid, pid, err)
		pma.RenderError(http.StatusInternalServerError, "Failed to update data in DB")
		return
	}
	//insert roles in request
	for _, rid := range req.Roles {
		err = dao.AddProjectMember(pid, mid, int(rid))
		if err != nil {
			log.Errorf("Failed to update DB to add project user role, project id: %d, user id: %d, role id: %d", pid, mid, rid)
			pma.RenderError(http.StatusInternalServerError, "Failed to update data in database")
			return
		}
	}
}