Skip to content

Commit

Permalink
fix authorities saving
Browse files Browse the repository at this point in the history
  • Loading branch information
mdachi-codes committed Dec 6, 2022
1 parent 690111b commit 17e81b0
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions lib/d2_touch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ import 'modules/metadata/dashboard/dashboard.module.dart';
import 'modules/metadata/data_element/data_element.module.dart';

class D2Touch {
static Future<void> initialize(
{String? databaseName,
bool? inMemory,
DatabaseFactory? databaseFactory}) async {
static Future<void> initialize({String? databaseName,
bool? inMemory,
DatabaseFactory? databaseFactory}) async {
final newDatabaseName = databaseName ?? await D2Touch.getDatabaseName();
if (newDatabaseName != null) {
DatabaseManager(
Expand All @@ -53,8 +52,8 @@ class D2Touch {

static Future<bool> isAuthenticated(
{Future<SharedPreferences>? sharedPreferenceInstance,
bool? inMemory,
DatabaseFactory? databaseFactory}) async {
bool? inMemory,
DatabaseFactory? databaseFactory}) async {
WidgetsFlutterBinding.ensureInitialized();
final databaseName = await D2Touch.getDatabaseName(
sharedPreferenceInstance: sharedPreferenceInstance);
Expand All @@ -77,27 +76,25 @@ class D2Touch {
{Future<SharedPreferences>? sharedPreferenceInstance}) async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs =
await (sharedPreferenceInstance ?? SharedPreferences.getInstance());
await (sharedPreferenceInstance ?? SharedPreferences.getInstance());
return prefs.getString('databaseName');
}

static Future<bool> setDatabaseName(
{required String databaseName,
Future<SharedPreferences>? sharedPreferenceInstance}) async {
static Future<bool> setDatabaseName({required String databaseName,
Future<SharedPreferences>? sharedPreferenceInstance}) async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs =
await (sharedPreferenceInstance ?? SharedPreferences.getInstance());
await (sharedPreferenceInstance ?? SharedPreferences.getInstance());
return prefs.setString('databaseName', databaseName);
}

static Future<LoginResponseStatus> logIn(
{required String username,
required String password,
required String url,
Future<SharedPreferences>? sharedPreferenceInstance,
bool? inMemory,
DatabaseFactory? databaseFactory,
Dio? dioTestClient}) async {
static Future<LoginResponseStatus> logIn({required String username,
required String password,
required String url,
Future<SharedPreferences>? sharedPreferenceInstance,
bool? inMemory,
DatabaseFactory? databaseFactory,
Dio? dioTestClient}) async {
WidgetsFlutterBinding.ensureInitialized();
HttpResponse userResponse = await HttpClient.get(
'me.json?fields=id,name,created,lastUpdated,birthday,gender,displayName,jobTitle,surname,employer,email,firstName,phoneNumber,nationality,userCredentials[code,id,name,lastLogin,displayName,username,userRoles[id,name,code]],organisationUnits[id,code,name],dataViewOrganisationUnits[id,code,name],userGroups[id,name],authorities,programs,dataSets',
Expand All @@ -114,7 +111,9 @@ class D2Touch {
return LoginResponseStatus.SERVER_ERROR;
}

final uri = Uri.parse(url).host;
final uri = Uri
.parse(url)
.host;
final String databaseName = '${username}_$uri';

await D2Touch.initialize(
Expand All @@ -125,7 +124,7 @@ class D2Touch {
await D2Touch.setDatabaseName(
databaseName: databaseName,
sharedPreferenceInstance:
sharedPreferenceInstance ?? SharedPreferences.getInstance());
sharedPreferenceInstance ?? SharedPreferences.getInstance());

UserQuery userQuery = UserQuery();

Expand Down Expand Up @@ -161,15 +160,16 @@ class D2Touch {
return logOutSuccess;
}

static Future<LoginResponseStatus> setToken(
{required String instanceUrl,
required Map<String, dynamic> userObject,
required Map<String, dynamic> tokenObject,
Future<SharedPreferences>? sharedPreferenceInstance,
bool? inMemory,
DatabaseFactory? databaseFactory,
Dio? dioTestClient}) async {
final uri = Uri.parse(instanceUrl).host;
static Future<LoginResponseStatus> setToken({required String instanceUrl,
required Map<String, dynamic> userObject,
required Map<String, dynamic> tokenObject,
Future<SharedPreferences>? sharedPreferenceInstance,
bool? inMemory,
DatabaseFactory? databaseFactory,
Dio? dioTestClient}) async {
final uri = Uri
.parse(instanceUrl)
.host;
final String databaseName = '$uri';
await D2Touch.initialize(
databaseName: databaseName,
Expand All @@ -179,10 +179,21 @@ class D2Touch {
await D2Touch.setDatabaseName(
databaseName: databaseName,
sharedPreferenceInstance:
sharedPreferenceInstance ?? SharedPreferences.getInstance());
sharedPreferenceInstance ?? SharedPreferences.getInstance());

AuthToken token = AuthToken.fromJson(tokenObject);

List<dynamic> authorities = [];

userObject['userCredentials']['userRoles'].forEach((role) {

List<dynamic> authoritiesToAdd = role["authorities"].map((auth) {
return auth as String;
}).toList();

authorities.addAll(authoritiesToAdd);
});

userObject['token'] = token.accessToken;
userObject['tokenType'] = token.tokenType;
userObject['tokenExpiry'] = token.expiresIn;
Expand All @@ -191,6 +202,7 @@ class D2Touch {
userObject['dirty'] = true;
userObject['baseUrl'] = instanceUrl;
userObject['authType'] = "token";
userObject['authorities'] = authorities;

final user = User.fromApi(userObject);
await UserQuery().setData(user).save();
Expand All @@ -212,7 +224,7 @@ class D2Touch {
static UserModule userModule = UserModule();

static OrganisationUnitModule organisationUnitModule =
OrganisationUnitModule();
OrganisationUnitModule();

static DataElementModule dataElementModule = DataElementModule();

Expand All @@ -223,7 +235,7 @@ class D2Touch {
static DashboardModule dashboardModule = DashboardModule();

static TrackedEntityInstanceModule trackerModule =
TrackedEntityInstanceModule();
TrackedEntityInstanceModule();

static AggregateModule aggregateModule = AggregateModule();

Expand Down

0 comments on commit 17e81b0

Please sign in to comment.