diff --git a/sources/Users/User.uc b/sources/Users/User.uc index 73fa259..beecfbd 100644 --- a/sources/Users/User.uc +++ b/sources/Users/User.uc @@ -40,7 +40,8 @@ var private JSONPointer persistentSettingsPointer; // Group names are stored in the lower register. var private array userGroups; // user groups loaded from database var private array localUserGroups; // user groups loaded from local files -var private LoggerAPI.Definition errNoUserDataDatabase; +var private LoggerAPI.Definition errNoUserDataDatabase, errCannotReadDB; +var private LoggerAPI.Definition errInvalidUserGroups; protected function Finalizer() { @@ -64,12 +65,17 @@ protected function Finalizer() */ public final function Initialize(UserID initID, int initKey) { + local DBReadTask groupsReadingTask; id = initID; key = initKey; if (initID != none) { initID.NewRef(); } LoadLocalGroups(); + groupsReadingTask = ReadPersistentData(P("Acedia"), P("UserGroups")); + if (groupsReadingTask != none) { + groupsReadingTask.connect = LoadDBGroups; + } } /** @@ -130,6 +136,59 @@ private final function LoadLocalGroups() _.memory.FreeMany(availableGroups); } +// Loads groups defined in database with user data +private final function LoadDBGroups( + Database.DBQueryResult result, + AcediaObject data, + Database source) +{ + local int i; + local MutableText nextGroup; + local ArrayList dbGroups; + + if (result != DBR_Success) + { + _.logger.Auto(errCannotReadDB); + return; + } + _.memory.FreeMany(userGroups); + userGroups.length = 0; + dbGroups = ArrayList(data); + if (dbGroups == none) + { + if (data != none) + { + _.logger.Auto(errInvalidUserGroups); + _.memory.Free(data); + } + return; + } + for (i = 0; i < dbGroups.GetLength(); i += 1) + { + nextGroup = dbGroups.GetMutableText(i); + if (nextGroup == none) { + continue; + } + if (!class'UserGroup'.static.Exists(nextGroup)) + { + nextGroup.FreeSelf(); + continue; + } + userGroups[userGroups.length] = nextGroup.IntoText(); + } + dbGroups.FreeSelf(); +} + +// Save current user groups into the user data database +private final function UpdateDBGroups() +{ + local ArrayList newDBData; + + newDBData = _.collections.NewArrayList(userGroups); + WritePersistentData(P("Acedia"), P("UserGroups"), newDBData); + newDBData.FreeSelf(); +} + /** * Adds caller user into new group, specified by `newGroup`. * This group must exist for the method to succeed. @@ -150,6 +209,7 @@ public final function AddGroup(Text newGroup) } } userGroups[userGroups.length] = newGroup.LowerCopy(); + UpdateDBGroups(); } /** @@ -172,6 +232,7 @@ public final function bool RemoveGroup(Text groupToRemove) { userGroups[i].FreeSelf(); userGroups.Remove(i, 1); + UpdateDBGroups(); return true; } } @@ -390,5 +451,7 @@ private function bool SetupDatabaseVariables() defaultproperties { - errNoUserDataDatabase = (l=LOG_Error,m="Failed to load persistent user database instance given by link \"%1\".") + errCannotReadDB = (l=LOG_Error,m="Failed to read user groups from persistent user database.") + errInvalidUserGroups = (l=LOG_Error,m="Invalid data is written as user groups array inside persistent user database.") + errNoUserDataDatabase = (l=LOG_Error,m="Failed to load persistent user database instance given by link \"%1\".") } \ No newline at end of file