Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save subeshb1/ff6358b39bf335ca2c1a3b6d4c947fae to your computer and use it in GitHub Desktop.
Save subeshb1/ff6358b39bf335ca2c1a3b6d4c947fae to your computer and use it in GitHub Desktop.
<?php
$DASHBOARD_ID = "<DASHBOARD_ID>";
// Check if MemberPress class is accessible
if(class_exists('MeprUtils')) {
$user = MeprUtils::get_currentuserinfo();
// Check if the current user information is found
if($user != null) {
// Setting default region the where the dashboard is located.
putenv('AWS_DEFAULT_REGION=' . 'us-east-1');
// Checking if the current user exists in quicksight.
$describe_user_cmd="aws quicksight describe-user --aws-account-id <ACCOUNT_ID> --namespace default --user-name QuickSightEmbedRole/$user->user_email";
$exit_code=null;
$output=null;
$exec1 = exec($describe_user_cmd, $output, $exit_code);
// Flag to check if user creation fails, only for the case when user doesn't exist.
$user_creation = true;
// If the user doesn't exist, returns error code other than 0.
if($exit_code != 0) {
// Create the user in quicksight if the user doesn't exist.
$register_quicksight_user = "aws quicksight register-user --aws-account-id <ACCOUNT_ID> --namespace default --identity-type IAM --iam-arn 'arn:aws:iam::<ACCOUNT_ID>:role/QuickSightEmbedRole' --user-role READER --session-name $user->user_email --email $user->user_email";
$exit_code=null;
$output=null;
$exec1 = exec($register_quicksight_user, $output, $exit_code);
// If user creation fails skipping dashboard generation.
if($exit_code != 0) {
$user_creation = false;
}
}
// If there is no problem in user creation or user already exists.
if($user_creation) {
// Assign user to a group to view dashboard.
$assign_group = "aws quicksight create-group-membership --aws-account-id=<ACCOUNT_ID> --namespace=default --group-name=wordpress --member-name='QuickSightEmbedRole/$user->user_email'";
$exit_code=null;
$output=null;
$exec1 = exec($assign_group, $output, $exit_code);
//If the group membership assignment is successful.
if($exit_code == 0) {
// Assume the users role to generate dashboard URL for each individual user.
$assume_user_role = "aws sts assume-role --role-arn 'arn:aws:iam::<ACCOUNT_ID>:role/QuickSightEmbedRole' --role-session-name $user->user_email";
$exit_code=null;
$output=null;
$exec1 = exec($assume_user_role, $output, $exit_code);
if($exit_code == 0) {
// Fetching Embed url
$sts_response = json_decode(implode("\n",$output),true);
$access_key = $sts_response["Credentials"]["AccessKeyId"];
$secret_key = $sts_response["Credentials"]["SecretAccessKey"];
$session_token = $sts_response["Credentials"]["SessionToken"];
$fetch_embed_url = "AWS_ACCESS_KEY_ID=$access_key AWS_SECRET_ACCESS_KEY=$secret_key AWS_SESSION_TOKEN=$session_token aws quicksight get-dashboard-embed-url --aws-account-id <ACCOUNT_ID> --dashboard-id $DASHBOARD_ID --identity-type IAM";
$exit_code=null;
$output=null;
$exec1 = exec($fetch_embed_url, $output, $exit_code);
if($exit_code == 0) {
$embed_response = json_decode(implode("\n",$output),true);
$embed_url = '"'.$embed_response["EmbedUrl"].'"';
$embed_url = preg_replace('/\s+/', '', $embed_url);
echo $embed_url;
}
}
} else {
// If group assign fails, don't generate dashboard.
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Basic Embed</title>
<script type="text/javascript" src="https://unpkg.com/amazon-quicksight-embedding-sdk@1.0.2/dist/quicksight-embedding-js-sdk.min.js"></script>
<script type="text/javascript">
function embedDashboard() {
var containerDiv = document.getElementById("dashboardContainer");
var params = {
url: [xyz-ips snippet="Quicksight-Dashboard"],
container: containerDiv,
height: "1100px",
width: "1200px"
};
var dashboard = QuickSightEmbedding.embedDashboard(params);
dashboard.on('error', function() {});
dashboard.on('load', function() {});
dashboard.setParameters({country: 'Canada'});
}
</script>
</head>
<body data-rsssl=1 data-rsssl=1 data-rsssl=1 data-rsssl=1 data-rsssl=1 onload="embedDashboard()">
<div id="dashboardContainer"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment