Java Code Examples for org.wso2.carbon.context.PrivilegedCarbonContext#getUsername()

The following examples show how to use org.wso2.carbon.context.PrivilegedCarbonContext#getUsername() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiPermissionFilter.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether the client is authorized with the given permission and action.
 * @param permission           Carbon permission that requires for the use
 * @param action               Carbon permission action that requires for the given permission.
 * @return boolean - true if user is authorized else return false.
 */
private boolean isUserAuthorized(String permission, String action) {
    PrivilegedCarbonContext context = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    String username = context.getUsername();
    try {
        UserRealm userRealm = APIUtil.getRealmService().getTenantUserRealm(PrivilegedCarbonContext
                            .getThreadLocalCarbonContext().getTenantId());
        String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(username);
        boolean status =  userRealm.getAuthorizationManager()
                .isUserAuthorized(tenantAwareUsername, permission, action);
        if (!status) {
            String[] roles = userRealm.getUserStoreManager().getRoleListOfUser(tenantAwareUsername);
            for (String role : roles) {
                if (role.equals(DEFAULT_ADMIN_ROLE)) {
                    return true;
                }
            }
        }
        return status;
    } catch (UserStoreException e) {
        String errorMsg = String.format("Unable to authorize the user : %s", username);
        log.error(errorMsg, e);
        return false;
    }
}
 
Example 2
Source File: AbstractCertificateDAOImpl.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void addCertificate(List<Certificate> certificates)
        throws CertificateManagementDAOException {
    Connection conn;
    PreparedStatement stmt = null;
    try {
        conn = this.getConnection();
        stmt = conn.prepareStatement(
                "INSERT INTO DM_DEVICE_CERTIFICATE (SERIAL_NUMBER, CERTIFICATE, TENANT_ID, USERNAME)"
                + " VALUES (?,?,?,?)");
        PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.
                                                                                          getThreadLocalCarbonContext();
        String username = threadLocalCarbonContext.getUsername();
        for (Certificate certificate : certificates) {
            // the serial number of the certificate used for its creation is set as its alias.
            String serialNumber = certificate.getSerial();
            if (serialNumber == null || serialNumber.isEmpty()) {
                serialNumber = String.valueOf(certificate.getCertificate().getSerialNumber());
            }
            byte[] bytes = Serializer.serialize(certificate.getCertificate());

            stmt.setString(1, serialNumber);
            stmt.setBytes(2, bytes);
            stmt.setInt(3, certificate.getTenantId());
            stmt.setString(4, username);
            stmt.addBatch();
        }
        stmt.executeBatch();
    } catch (SQLException | IOException e) {
        throw new CertificateManagementDAOException("Error occurred while saving certificates. "
                , e);
    } finally {
        CertificateManagementDAOUtil.cleanupResources(stmt, null);
    }
}
 
Example 3
Source File: DeviceMgtAPIUtils.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
public static String getAuthenticatedUser() {
    PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    String username = threadLocalCarbonContext.getUsername();
    String tenantDomain = threadLocalCarbonContext.getTenantDomain();
    if (username != null && username.endsWith(tenantDomain)) {
        return username.substring(0, username.lastIndexOf("@"));
    }
    return username;
}
 
Example 4
Source File: DeviceMgtAPIUtils.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
public static String getAuthenticatedUser() {
    PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    String username = threadLocalCarbonContext.getUsername();
    String tenantDomain = threadLocalCarbonContext.getTenantDomain();
    if (username != null && username.endsWith(tenantDomain)) {
        return username.substring(0, username.lastIndexOf("@"));
    }
    return username;
}
 
Example 5
Source File: APIUtil.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
public static String getAuthenticatedUser() {
    PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    String username = threadLocalCarbonContext.getUsername();
    String tenantDomain = threadLocalCarbonContext.getTenantDomain();
    if (username.endsWith(tenantDomain)) {
        return username.substring(0, username.lastIndexOf("@"));
    }
    return username;
}
 
Example 6
Source File: APIUtil.java    From product-iots with Apache License 2.0 5 votes vote down vote up
public static String getAuthenticatedUser() {
    PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    String username = threadLocalCarbonContext.getUsername();
    String tenantDomain = threadLocalCarbonContext.getTenantDomain();
    if (username.endsWith(tenantDomain)) {
        return username.substring(0, username.lastIndexOf("@"));
    }
    return username;
}
 
Example 7
Source File: APIUtil.java    From product-iots with Apache License 2.0 5 votes vote down vote up
/**
 * @return username of the current user
 */
public static String getAuthenticatedUser() {
    PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    String username = threadLocalCarbonContext.getUsername();
    String tenantDomain = threadLocalCarbonContext.getTenantDomain();
    if (username.endsWith(tenantDomain)) {
        return username.substring(0, username.lastIndexOf("@"));
    }
    return username;
}
 
Example 8
Source File: APIUtil.java    From product-iots with Apache License 2.0 5 votes vote down vote up
public static String getAuthenticatedUser() {
    PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    String username = threadLocalCarbonContext.getUsername();
    String tenantDomain = threadLocalCarbonContext.getTenantDomain();
    if (username.endsWith(tenantDomain)) {
        return username.substring(0, username.lastIndexOf("@"));
    }
    return username;
}