misp-bump/app/src/main/java/lu/circl/mispbump/activities/LoginActivity.java

213 lines
7.7 KiB
Java
Raw Normal View History

2019-05-29 14:52:00 +02:00
package lu.circl.mispbump.activities;
2019-05-27 16:06:07 +02:00
2019-07-24 12:06:52 +02:00
2019-05-27 16:06:07 +02:00
import android.content.Intent;
import android.net.Uri;
2019-05-27 16:06:07 +02:00
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputLayout;
2019-05-27 16:06:07 +02:00
import java.util.Objects;
2019-05-29 14:52:00 +02:00
import lu.circl.mispbump.R;
import lu.circl.mispbump.auxiliary.DialogManager;
2019-06-19 20:01:49 +02:00
import lu.circl.mispbump.auxiliary.MispRestClient;
import lu.circl.mispbump.auxiliary.PreferenceManager;
2019-06-19 20:01:49 +02:00
import lu.circl.mispbump.models.restModels.Organisation;
import lu.circl.mispbump.models.restModels.Role;
2019-06-19 20:01:49 +02:00
import lu.circl.mispbump.models.restModels.User;
2019-05-27 16:06:07 +02:00
2019-07-24 12:06:52 +02:00
2019-05-27 16:06:07 +02:00
public class LoginActivity extends AppCompatActivity {
2019-06-04 20:48:24 +02:00
private PreferenceManager preferenceManager;
2019-07-05 03:45:04 +02:00
2019-05-27 16:06:07 +02:00
private ConstraintLayout constraintLayout;
private TextInputLayout serverAuthkey;
2019-06-04 20:48:24 +02:00
private TextInputLayout serverUrl;
2019-05-27 16:06:07 +02:00
private ProgressBar progressBar;
2019-06-04 20:48:24 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
2019-07-05 03:45:04 +02:00
preferenceManager = PreferenceManager.getInstance(this);
getWindow().setStatusBarColor(getColor(R.color.colorPrimary));
initializeViews();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
2019-06-04 20:48:24 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_login_help) {
DialogManager.loginHelpDialog(LoginActivity.this);
return true;
}
return super.onOptionsItemSelected(item);
}
2019-07-05 03:45:04 +02:00
private void initializeViews() {
2019-07-05 03:45:04 +02:00
constraintLayout = findViewById(R.id.rootLayout);
2019-06-04 20:48:24 +02:00
Toolbar myToolbar = findViewById(R.id.appbar);
setSupportActionBar(myToolbar);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(false);
2019-07-05 03:45:04 +02:00
ab.setDisplayShowTitleEnabled(false);
2019-06-04 20:48:24 +02:00
}
Button downloadInfoButton = findViewById(R.id.login_download_button);
2019-07-22 13:17:49 +02:00
downloadInfoButton.setOnClickListener(onLogin);
2019-06-04 20:48:24 +02:00
2019-06-19 17:24:00 +02:00
serverUrl = findViewById(R.id.login_server_url);
serverAuthkey = findViewById(R.id.login_automation_key);
progressBar = findViewById(R.id.login_progressbar);
2019-06-04 20:48:24 +02:00
}
/**
2019-07-22 13:17:49 +02:00
* Called when the user clicks on the login button.
2019-06-04 20:48:24 +02:00
*/
2019-07-22 13:17:49 +02:00
private View.OnClickListener onLogin = new View.OnClickListener() {
2019-05-27 16:06:07 +02:00
@Override
public void onClick(View v) {
2019-07-05 03:45:04 +02:00
final String url = Objects.requireNonNull(serverUrl.getEditText()).getText().toString();
final String authkey = Objects.requireNonNull(serverAuthkey.getEditText()).getText().toString();
2019-05-27 16:06:07 +02:00
boolean error = false;
serverUrl.setError(null);
serverAuthkey.setError(null);
2019-05-27 16:06:07 +02:00
2019-05-29 14:52:00 +02:00
if (!isValidUrl(url)) {
2019-05-27 16:06:07 +02:00
error = true;
serverUrl.setError("Invalid Server URL");
}
2019-05-29 14:52:00 +02:00
if (!isValidAutomationKey(authkey)) {
2019-05-27 16:06:07 +02:00
error = true;
serverAuthkey.setError("Invalid automation key");
2019-05-27 16:06:07 +02:00
}
if (error) {
return;
}
2019-07-14 18:18:54 +02:00
final MispRestClient mispRestClient = MispRestClient.getInstance(url, authkey);
2019-05-27 16:06:07 +02:00
// display progress bar
progressBar.setVisibility(View.VISIBLE);
// get my user information and the organisation associated with my user
mispRestClient.isAvailable(new MispRestClient.AvailableCallback() {
2019-05-27 16:06:07 +02:00
@Override
public void available() {
mispRestClient.getRoles(new MispRestClient.AllRolesCallback() {
@Override
2019-07-17 12:41:24 +02:00
public void success(final Role[] roles) {
preferenceManager.setRoles(roles);
2019-07-17 12:41:24 +02:00
mispRestClient.getMyUser(new MispRestClient.UserCallback() {
@Override
2019-07-17 12:41:24 +02:00
public void success(final User user) {
2019-08-22 18:09:45 +02:00
preferenceManager.setMyUser(user);
2019-07-22 13:17:49 +02:00
for (Role role : roles) {
2019-08-22 18:09:45 +02:00
if (role.getId().equals(user.getRoleId())) {
2019-07-17 12:41:24 +02:00
if (!role.getPermAdmin()) {
progressBar.setVisibility(View.GONE);
2019-07-22 13:17:49 +02:00
Snackbar.make(constraintLayout, "No admin is associated with this authkey.", Snackbar.LENGTH_LONG).show();
2019-07-17 12:41:24 +02:00
return;
}
}
}
2019-08-22 18:09:45 +02:00
mispRestClient.getOrganisation(user.getRoleId(), new MispRestClient.OrganisationCallback() {
2019-07-17 12:41:24 +02:00
@Override
public void success(Organisation organisation) {
2019-08-22 18:09:45 +02:00
preferenceManager.setMyOrganisation(organisation);
preferenceManager.setUserCredentials(url, authkey);
2019-07-17 12:41:24 +02:00
progressBar.setVisibility(View.GONE);
2019-07-17 12:41:24 +02:00
Intent home = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(home);
finish();
}
@Override
public void failure(String error) {
progressBar.setVisibility(View.GONE);
Snackbar.make(constraintLayout, error, Snackbar.LENGTH_LONG).show();
}
});
}
@Override
public void failure(String error) {
progressBar.setVisibility(View.GONE);
Snackbar.make(constraintLayout, error, Snackbar.LENGTH_LONG).show();
}
});
2019-05-27 16:06:07 +02:00
}
@Override
public void failure(String error) {
progressBar.setVisibility(View.GONE);
Snackbar.make(constraintLayout, error, Snackbar.LENGTH_LONG).show();
}
});
}
@Override
public void unavailable(String error) {
2019-05-27 16:06:07 +02:00
progressBar.setVisibility(View.GONE);
2019-07-17 12:41:24 +02:00
Snackbar.make(constraintLayout, error, Snackbar.LENGTH_LONG).show();
2019-05-27 16:06:07 +02:00
}
});
}
};
2019-06-04 20:48:24 +02:00
/**
* @param url url to check
* @return true if valid else false
2019-06-04 20:48:24 +02:00
*/
2019-05-27 16:06:07 +02:00
private boolean isValidUrl(String url) {
Uri uri = Uri.parse(url);
if (uri == null) {
return false;
}
return uri.getScheme() != null;
2019-05-27 16:06:07 +02:00
}
2019-06-04 20:48:24 +02:00
/**
* @param automationKey the key to check
* @return true if not empty else false
2019-06-04 20:48:24 +02:00
*/
2019-05-27 16:06:07 +02:00
private boolean isValidAutomationKey(String automationKey) {
return !automationKey.isEmpty();
2019-05-27 16:06:07 +02:00
}
}