packages/udaru-core/lib/ops/policyOps.js
function listAllUserPolicies ({ userId, organizationId }, cb) {
      let promise = null
      if (typeof cb !== 'function') [promise, cb] = asyncify()

      const rootOrgId = config.get('authorization.superUser.organization.id')
      const sql = SQL`
        WITH user_teams AS (
          SELECT id FROM teams WHERE path @> (
            SELECT array_agg(path) FROM teams
            INNER JOIN team_members tm
            ON tm.team_id = teams.id
            WHERE tm.user_id = ${userId}
          )
        ),
        policies_from_teams AS (
          SELECT
            policy_id, variables
          FROM
            team_policies
          WHERE
            team_id IN (SELECT id FROM user_teams)
        ),
        policies_from_user AS (
          SELECT
            policy_id, variables
          FROM
            user_policies
          WHERE
            user_id = ${userId}
        ),
        policies_from_organization AS (
          SELECT
            op.policy_id, variables
          FROM
            organization_policies op
          LEFT JOIN
            users u ON u.org_id = op.org_id
          WHERE
            u.id = ${userId}
        ),
        is_root_user AS (
          SELECT FROM users WHERE id=${userId} AND org_id = ${rootOrgId}
        )
        SELECT
          id,
          version,
          name,
          statements,
          policies_from_user.variables AS variables
        FROM
          policies
        INNER JOIN
          policies_from_user
        ON
          policies.id = policies_from_user.policy_id
        WHERE (
            org_id = ${organizationId}
          OR (
              EXISTS (SELECT FROM is_root_user)
            AND
              org_id = ${rootOrgId}
          )
          OR org_id IS NULL
        )
        UNION
        SELECT
          id,
          version,
          name,
          statements,
          policies_from_teams.variables AS variables
        FROM
          policies
        INNER JOIN
          policies_from_teams
        ON
          policies.id = policies_from_teams.policy_id
        WHERE (
            org_id = ${organizationId}
          OR (
              EXISTS (SELECT FROM is_root_user)
            AND
              org_id = ${rootOrgId}
          )
          OR org_id IS NULL
        )
        UNION
        SELECT
          id,
          version,
          name,
          statements,
          policies_from_organization.variables
        FROM
          policies
        INNER JOIN
          policies_from_organization
        ON
          policies.id = policies_from_organization.policy_id
        WHERE (
            org_id = ${organizationId}
          OR (
              EXISTS (SELECT FROM is_root_user)
            AND
              org_id = ${rootOrgId}
          )
          OR org_id IS NULL
        )
      `

      db.query(sql, function (err, result) {
        if (err) return cb(Boom.badImplementation(err))
        cb(null, result.rows.map(mapping.policy.iam))
      })

      return promise
    }
function listAllUserPolicies ({ userId, organizationId }, cb) {
      let promise = null
      if (typeof cb !== 'function') [promise, cb] = asyncify()

      const rootOrgId = config.get('authorization.superUser.organization.id')
      const sql = SQL`
        WITH user_teams AS (
          SELECT id FROM teams WHERE path @> (
            SELECT array_agg(path) FROM teams
            INNER JOIN team_members tm
            ON tm.team_id = teams.id
            WHERE tm.user_id = ${userId}
          )
        ),
        policies_from_teams AS (
          SELECT
            policy_id, variables
          FROM
            team_policies
          WHERE
            team_id IN (SELECT id FROM user_teams)
        ),
        policies_from_user AS (
          SELECT
            policy_id, variables
          FROM
            user_policies
          WHERE
            user_id = ${userId}
        ),
        policies_from_organization AS (
          SELECT
            op.policy_id, variables
          FROM
            organization_policies op
          LEFT JOIN
            users u ON u.org_id = op.org_id
          WHERE
            u.id = ${userId}
        ),
        is_root_user AS (
          SELECT FROM users WHERE id=${userId} AND org_id = ${rootOrgId}
        )
        SELECT
          id,
          version,
          name,
          statements,
          policies_from_user.variables AS variables
        FROM
          policies
        INNER JOIN
          policies_from_user
        ON
          policies.id = policies_from_user.policy_id
        WHERE (
            org_id = ${organizationId}
          OR (
              EXISTS (SELECT FROM is_root_user)
            AND
              org_id = ${rootOrgId}
          )
          OR org_id IS NULL
        )
        UNION
        SELECT
          id,
          version,
          name,
          statements,
          policies_from_teams.variables AS variables
        FROM
          policies
        INNER JOIN
          policies_from_teams
        ON
          policies.id = policies_from_teams.policy_id
        WHERE (
            org_id = ${organizationId}
          OR (
              EXISTS (SELECT FROM is_root_user)
            AND
              org_id = ${rootOrgId}
          )
          OR org_id IS NULL
        )
        UNION
        SELECT
          id,
          version,
          name,
          statements,
          policies_from_organization.variables
        FROM
          policies
        INNER JOIN
          policies_from_organization
        ON
          policies.id = policies_from_organization.policy_id
        WHERE (
            org_id = ${organizationId}
          OR (
              EXISTS (SELECT FROM is_root_user)
            AND
              org_id = ${rootOrgId}
          )
          OR org_id IS NULL
        )
      `

      db.query(sql, function (err, result) {
        if (err) return cb(Boom.badImplementation(err))
        cb(null, result.rows.map(mapping.policy.iam))
      })

      return promise
    }
function listAllUserPolicies ( { userId , organizationId } , cb ) { let promise = null if ( typeof cb !== 'function' ) [ promise , cb ] = asyncify ( ) const rootOrgId = config . get ( 'authorization.superUser.organization.id' ) const sql = SQL ` ${ userId } ${ userId } ${ userId } ${ userId } ${ rootOrgId } ${ organizationId } ${ rootOrgId } ${ organizationId } ${ rootOrgId } ${ organizationId } ${ rootOrgId } ` db . query ( sql , function ( err , result ) { if ( err ) return cb ( Boom . badImplementation ( err ) ) cb ( null , result . rows . map ( mapping . policy . iam ) ) } ) return promise }
List all user policies

@param  {Object}   params { userId, organizationId }
@param  {Function} cb
List all user policies