From 0fe7f7a79b47917b9d7ed93a33d8f0cb56059e63 Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Sun, 22 Jan 2023 06:11:28 -0700 Subject: [PATCH] set policy --- .../page/s3manager/BucketPolicy.java | 97 ++++++++++++++++++- .../asgardius/page/s3manager/CorsConfig.java | 18 ++-- .../res/layout/activity_bucket_policy.xml | 15 +-- app/src/main/res/values-es/strings.xml | 6 +- app/src/main/res/values/strings.xml | 6 +- 5 files changed, 117 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/asgardius/page/s3manager/BucketPolicy.java b/app/src/main/java/asgardius/page/s3manager/BucketPolicy.java index b6bca02..0f20536 100644 --- a/app/src/main/java/asgardius/page/s3manager/BucketPolicy.java +++ b/app/src/main/java/asgardius/page/s3manager/BucketPolicy.java @@ -2,6 +2,7 @@ package asgardius.page.s3manager; import android.os.Bundle; import android.view.View; +import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -24,6 +25,7 @@ public class BucketPolicy extends AppCompatActivity { boolean style; ProgressBar simpleProgressBar; TextView permission; + Button setpublic, setprivate; @Override protected void onCreate(Bundle savedInstanceState) { @@ -38,6 +40,7 @@ public class BucketPolicy extends AppCompatActivity { bucket = getIntent().getStringExtra("bucket"); style = getIntent().getBooleanExtra("style", false); location = getIntent().getStringExtra("region"); + getSupportActionBar().setTitle(bucket+"/"); region = Region.getRegion(location); s3ClientOptions = S3ClientOptions.builder().build(); myCredentials = new BasicAWSCredentials(username, password); @@ -49,6 +52,8 @@ public class BucketPolicy extends AppCompatActivity { s3client.setEndpoint(endpoint); s3ClientOptions.setPathStyleAccess(style); s3client.setS3ClientOptions(s3ClientOptions); + setprivate = (Button)findViewById(R.id.set_private); + setpublic = (Button)findViewById(R.id.set_public); Thread getPolicy = new Thread(new Runnable() { @Override @@ -67,7 +72,9 @@ public class BucketPolicy extends AppCompatActivity { permission.setText(getResources().getString(R.string.public_bucket)); } else { permission.setText(getResources().getString(R.string.custom_policy)); + setpublic.setVisibility(View.VISIBLE); } + setprivate.setVisibility(View.VISIBLE); simpleProgressBar.setVisibility(View.INVISIBLE); } }); @@ -79,8 +86,9 @@ public class BucketPolicy extends AppCompatActivity { @Override public void run() { if(e.toString().contains("NoSuchBucketPolicy")) { - simpleProgressBar.setVisibility(View.INVISIBLE); permission.setText(getResources().getString(R.string.private_bucket)); + setpublic.setVisibility(View.VISIBLE); + simpleProgressBar.setVisibility(View.INVISIBLE); } else { Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show(); @@ -93,5 +101,92 @@ public class BucketPolicy extends AppCompatActivity { } }); getPolicy.start(); + setpublic.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View view) { + //buttonaction + simpleProgressBar.setVisibility(View.VISIBLE); + setPublic(); + } + }); + setprivate.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View view) { + //buttonaction + simpleProgressBar.setVisibility(View.VISIBLE); + setPrivate(); + } + }); + } + private void setPublic() { + Thread setPublic = new Thread(new Runnable() { + + @Override + public void run() { + try { + //Your code goes here + String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::" + +bucket+"\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::"+bucket+"/*\"]}]}"; + s3client.setBucketPolicy(bucket, policy); + + runOnUiThread(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_ok), Toast.LENGTH_SHORT).show(); + finish(); + } + }); + + } catch (Exception e) { + e.printStackTrace(); + runOnUiThread(new Runnable() { + + @Override + public void run() { + simpleProgressBar.setVisibility(View.INVISIBLE); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_error), Toast.LENGTH_SHORT).show(); + } + }); + //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + } + } + }); + setPublic.start(); + } + + private void setPrivate() { + Thread setPrivate = new Thread(new Runnable() { + + @Override + public void run() { + try { + //Your code goes here + s3client.deleteBucketPolicy(bucket); + + runOnUiThread(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_ok), Toast.LENGTH_SHORT).show(); + finish(); + } + }); + + } catch (Exception e) { + e.printStackTrace(); + runOnUiThread(new Runnable() { + + @Override + public void run() { + simpleProgressBar.setVisibility(View.INVISIBLE); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_error), Toast.LENGTH_SHORT).show(); + } + }); + //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + } + } + }); + setPrivate.start(); } } \ No newline at end of file diff --git a/app/src/main/java/asgardius/page/s3manager/CorsConfig.java b/app/src/main/java/asgardius/page/s3manager/CorsConfig.java index 38d114f..89f8dae 100644 --- a/app/src/main/java/asgardius/page/s3manager/CorsConfig.java +++ b/app/src/main/java/asgardius/page/s3manager/CorsConfig.java @@ -160,7 +160,7 @@ public class CorsConfig extends AppCompatActivity { }); } - public void setAllowall() { + private void setAllowall() { Thread addAll = new Thread(new Runnable() { @Override @@ -179,7 +179,7 @@ public class CorsConfig extends AppCompatActivity { @Override public void run() { - Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_ok), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_ok), Toast.LENGTH_SHORT).show(); finish(); } }); @@ -191,7 +191,7 @@ public class CorsConfig extends AppCompatActivity { @Override public void run() { simpleProgressBar.setVisibility(View.INVISIBLE); - Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_error), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_error), Toast.LENGTH_SHORT).show(); } }); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); @@ -201,7 +201,7 @@ public class CorsConfig extends AppCompatActivity { addAll.start(); } - public void setAllowpdf() { + private void setAllowpdf() { Thread addPdf = new Thread(new Runnable() { @Override @@ -220,7 +220,7 @@ public class CorsConfig extends AppCompatActivity { @Override public void run() { - Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_ok), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_ok), Toast.LENGTH_SHORT).show(); finish(); } }); @@ -232,7 +232,7 @@ public class CorsConfig extends AppCompatActivity { @Override public void run() { simpleProgressBar.setVisibility(View.INVISIBLE); - Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_error), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_error), Toast.LENGTH_SHORT).show(); } }); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); @@ -242,7 +242,7 @@ public class CorsConfig extends AppCompatActivity { addPdf.start(); } - public void setDeletecors() { + private void setDeletecors() { Thread delCors = new Thread(new Runnable() { @Override @@ -255,7 +255,7 @@ public class CorsConfig extends AppCompatActivity { @Override public void run() { - Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_ok), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_ok), Toast.LENGTH_SHORT).show(); finish(); } }); @@ -267,7 +267,7 @@ public class CorsConfig extends AppCompatActivity { @Override public void run() { simpleProgressBar.setVisibility(View.INVISIBLE); - Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_error), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_error), Toast.LENGTH_SHORT).show(); } }); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/res/layout/activity_bucket_policy.xml b/app/src/main/res/layout/activity_bucket_policy.xml index 79188a6..aa792d5 100644 --- a/app/src/main/res/layout/activity_bucket_policy.xml +++ b/app/src/main/res/layout/activity_bucket_policy.xml @@ -24,24 +24,17 @@ android:textSize="25sp"/>