Check rights

This commit is contained in:
Thomas 2020-12-30 16:20:49 +01:00
parent bbfa278d6b
commit 57327a8ad0
8 changed files with 47 additions and 4 deletions

View File

@ -387,6 +387,7 @@ public class LoginActivity extends AppCompatActivity {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token.getAccess_token()); editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token.getAccess_token());
editor.putString(Helper.PREF_SOFTWARE, software != null && software.toUpperCase().trim().compareTo("PEERTUBE") != 0 ? software : null);
editor.putString(Helper.PREF_INSTANCE, host); editor.putString(Helper.PREF_INSTANCE, host);
editor.commit(); editor.commit();
//Update the account with the token; //Update the account with the token;

View File

@ -939,7 +939,8 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
public enum TypeOfConnection { public enum TypeOfConnection {
UNKNOWN, UNKNOWN,
NORMAL, NORMAL,
SURFING SURFING,
REMOTE_ACCOUNT
} }
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

View File

@ -137,9 +137,10 @@ public class WebviewConnectActivity extends AppCompatActivity {
if (token != null) { if (token != null) {
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token.getAccess_token()); editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token.getAccess_token());
editor.putString(Helper.PREF_SOFTWARE, null);
editor.putString(Helper.PREF_INSTANCE, instance); editor.putString(Helper.PREF_INSTANCE, instance);
editor.apply(); editor.apply();
updateCredential(WebviewConnectActivity.this, token.getAccess_token(), clientId, clientSecret, token.getRefresh_token(), new URL(url).getHost()); updateCredential(WebviewConnectActivity.this, token.getAccess_token(), clientId, clientSecret, token.getRefresh_token(), new URL(url).getHost(), null);
finish(); finish();
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -241,6 +241,7 @@ public class RetrofitPeertubeAPI {
SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, tokenReply.getAccess_token()); editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, tokenReply.getAccess_token());
editor.putString(Helper.PREF_SOFTWARE, null);
editor.apply(); editor.apply();
SQLiteDatabase db = Sqlite.getInstance(_context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SQLiteDatabase db = Sqlite.getInstance(_context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new AccountDAO(_context, db).updateAccountToken(tokenReply); new AccountDAO(_context, db).updateAccountToken(tokenReply);

View File

@ -77,7 +77,7 @@ public class AccountData {
private String client_id; private String client_id;
private String client_secret; private String client_secret;
private String refresh_token; private String refresh_token;
private String software;
public Account() { public Account() {
} }
@ -241,6 +241,14 @@ public class AccountData {
this.userId = userId; this.userId = userId;
} }
public String getSoftware() {
return software;
}
public void setSoftware(String software) {
this.software = software;
}
@Override @Override
public int describeContents() { public int describeContents() {
return 0; return 0;

View File

@ -114,6 +114,7 @@ public class Helper {
public static final String OAUTH_SCOPES_MASTODON = "read write follow"; public static final String OAUTH_SCOPES_MASTODON = "read write follow";
public static final String REDIRECT_CONTENT = "urn:ietf:wg:oauth:2.0:oob"; public static final String REDIRECT_CONTENT = "urn:ietf:wg:oauth:2.0:oob";
public static final String PREF_KEY_OAUTH_TOKEN = "oauth_token"; public static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
public static final String PREF_SOFTWARE = "pref_software";
public static final Pattern redirectPattern = Pattern.compile("externalAuthToken=(\\w+)&username=([\\w.-]+)"); public static final Pattern redirectPattern = Pattern.compile("externalAuthToken=(\\w+)&username=([\\w.-]+)");
public static final String SET_VIDEO_CACHE = "set_video_cache"; public static final String SET_VIDEO_CACHE = "set_video_cache";
public static final String RECEIVE_CAST_SETTINGS = "receive_cast_settings"; public static final String RECEIVE_CAST_SETTINGS = "receive_cast_settings";
@ -484,9 +485,36 @@ public class Helper {
* @return boolean * @return boolean
*/ */
public static boolean isLoggedIn(Context context) { public static boolean isLoggedIn(Context context) {
return isLoggedInType(context) == MainActivity.TypeOfConnection.NORMAL;
}
/**
* Some actions like following writing comment can be performed with remote accounts
*
* @param context Context
* @return boolean
*/
public static boolean canMakeAction(Context context) {
return (isLoggedInType(context) == MainActivity.TypeOfConnection.NORMAL || isLoggedInType(context) == MainActivity.TypeOfConnection.REMOTE_ACCOUNT);
}
/**
* Returns boolean depending if the user is authenticated
*
* @param context Context
* @return MainActivity.TypeOfConnection
*/
private static MainActivity.TypeOfConnection isLoggedInType(Context context) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String prefKeyOauthTokenT = sharedpreferences.getString(PREF_KEY_OAUTH_TOKEN, null); String prefKeyOauthTokenT = sharedpreferences.getString(PREF_KEY_OAUTH_TOKEN, null);
return (prefKeyOauthTokenT != null); String prefSoftware = sharedpreferences.getString(PREF_SOFTWARE, null);
if (prefKeyOauthTokenT != null && prefSoftware == null) {
return MainActivity.TypeOfConnection.NORMAL;
} else if (prefKeyOauthTokenT != null) {
return MainActivity.TypeOfConnection.REMOTE_ACCOUNT;
} else {
return MainActivity.TypeOfConnection.UNKNOWN;
}
} }

View File

@ -55,6 +55,7 @@ public class SwitchAccountHelper {
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, account.getToken()); editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, account.getToken());
editor.putString(Helper.PREF_SOFTWARE, account.getSoftware() != null && account.getSoftware().trim().toUpperCase().compareTo("PEERTUBE") != 0 ? account.getSoftware() : null);
editor.putString(Helper.PREF_INSTANCE, account.getHost()); editor.putString(Helper.PREF_INSTANCE, account.getHost());
editor.putString(Helper.PREF_KEY_ID, account.getId()); editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername()); editor.putString(Helper.PREF_KEY_NAME, account.getUsername());

View File

@ -317,6 +317,7 @@ public class AccountDAO {
account.setCreatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT)))); account.setCreatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
account.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE))); account.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN))); account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN)));
account.setSoftware(c.getString(c.getColumnIndex(Sqlite.COL_SOFTWARE)));
account.setClient_id(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_ID))); account.setClient_id(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_ID)));
account.setClient_secret(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_SECRET))); account.setClient_secret(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_SECRET)));
account.setRefresh_token(c.getString(c.getColumnIndex(Sqlite.COL_REFRESH_TOKEN))); account.setRefresh_token(c.getString(c.getColumnIndex(Sqlite.COL_REFRESH_TOKEN)));
@ -357,6 +358,7 @@ public class AccountDAO {
account.setCreatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT)))); account.setCreatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
account.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE))); account.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN))); account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN)));
account.setSoftware(c.getString(c.getColumnIndex(Sqlite.COL_SOFTWARE)));
account.setClient_id(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_ID))); account.setClient_id(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_ID)));
account.setClient_secret(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_SECRET))); account.setClient_secret(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_SECRET)));
account.setRefresh_token(c.getString(c.getColumnIndex(Sqlite.COL_REFRESH_TOKEN))); account.setRefresh_token(c.getString(c.getColumnIndex(Sqlite.COL_REFRESH_TOKEN)));