From 17e81b09847204027eb12a617c6618f8b5b27df7 Mon Sep 17 00:00:00 2001 From: Jesse-Justin Mdachi Date: Tue, 6 Dec 2022 09:14:01 +0300 Subject: [PATCH] fix authorities saving --- lib/d2_touch.dart | 78 +++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/lib/d2_touch.dart b/lib/d2_touch.dart index a58ccf2d..6a8bb6f1 100644 --- a/lib/d2_touch.dart +++ b/lib/d2_touch.dart @@ -25,10 +25,9 @@ import 'modules/metadata/dashboard/dashboard.module.dart'; import 'modules/metadata/data_element/data_element.module.dart'; class D2Touch { - static Future initialize( - {String? databaseName, - bool? inMemory, - DatabaseFactory? databaseFactory}) async { + static Future initialize({String? databaseName, + bool? inMemory, + DatabaseFactory? databaseFactory}) async { final newDatabaseName = databaseName ?? await D2Touch.getDatabaseName(); if (newDatabaseName != null) { DatabaseManager( @@ -53,8 +52,8 @@ class D2Touch { static Future isAuthenticated( {Future? sharedPreferenceInstance, - bool? inMemory, - DatabaseFactory? databaseFactory}) async { + bool? inMemory, + DatabaseFactory? databaseFactory}) async { WidgetsFlutterBinding.ensureInitialized(); final databaseName = await D2Touch.getDatabaseName( sharedPreferenceInstance: sharedPreferenceInstance); @@ -77,27 +76,25 @@ class D2Touch { {Future? sharedPreferenceInstance}) async { WidgetsFlutterBinding.ensureInitialized(); SharedPreferences prefs = - await (sharedPreferenceInstance ?? SharedPreferences.getInstance()); + await (sharedPreferenceInstance ?? SharedPreferences.getInstance()); return prefs.getString('databaseName'); } - static Future setDatabaseName( - {required String databaseName, - Future? sharedPreferenceInstance}) async { + static Future setDatabaseName({required String databaseName, + Future? sharedPreferenceInstance}) async { WidgetsFlutterBinding.ensureInitialized(); SharedPreferences prefs = - await (sharedPreferenceInstance ?? SharedPreferences.getInstance()); + await (sharedPreferenceInstance ?? SharedPreferences.getInstance()); return prefs.setString('databaseName', databaseName); } - static Future logIn( - {required String username, - required String password, - required String url, - Future? sharedPreferenceInstance, - bool? inMemory, - DatabaseFactory? databaseFactory, - Dio? dioTestClient}) async { + static Future logIn({required String username, + required String password, + required String url, + Future? 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', @@ -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( @@ -125,7 +124,7 @@ class D2Touch { await D2Touch.setDatabaseName( databaseName: databaseName, sharedPreferenceInstance: - sharedPreferenceInstance ?? SharedPreferences.getInstance()); + sharedPreferenceInstance ?? SharedPreferences.getInstance()); UserQuery userQuery = UserQuery(); @@ -161,15 +160,16 @@ class D2Touch { return logOutSuccess; } - static Future setToken( - {required String instanceUrl, - required Map userObject, - required Map tokenObject, - Future? sharedPreferenceInstance, - bool? inMemory, - DatabaseFactory? databaseFactory, - Dio? dioTestClient}) async { - final uri = Uri.parse(instanceUrl).host; + static Future setToken({required String instanceUrl, + required Map userObject, + required Map tokenObject, + Future? sharedPreferenceInstance, + bool? inMemory, + DatabaseFactory? databaseFactory, + Dio? dioTestClient}) async { + final uri = Uri + .parse(instanceUrl) + .host; final String databaseName = '$uri'; await D2Touch.initialize( databaseName: databaseName, @@ -179,10 +179,21 @@ class D2Touch { await D2Touch.setDatabaseName( databaseName: databaseName, sharedPreferenceInstance: - sharedPreferenceInstance ?? SharedPreferences.getInstance()); + sharedPreferenceInstance ?? SharedPreferences.getInstance()); AuthToken token = AuthToken.fromJson(tokenObject); + List authorities = []; + + userObject['userCredentials']['userRoles'].forEach((role) { + + List 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; @@ -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(); @@ -212,7 +224,7 @@ class D2Touch { static UserModule userModule = UserModule(); static OrganisationUnitModule organisationUnitModule = - OrganisationUnitModule(); + OrganisationUnitModule(); static DataElementModule dataElementModule = DataElementModule(); @@ -223,7 +235,7 @@ class D2Touch { static DashboardModule dashboardModule = DashboardModule(); static TrackedEntityInstanceModule trackerModule = - TrackedEntityInstanceModule(); + TrackedEntityInstanceModule(); static AggregateModule aggregateModule = AggregateModule();