unit Safety4D; interface uses Safety4D.Interfaces, System.JSON; type TSafety4D = class(TInterfacedObject, iSafety4D) private FResources : iSafety4DResources; FGroupPermission : iSafety4DGroup; FuserKey : iSafety4DUserKey; FValidation : iSafety4DValidation; procedure __loadResourcesJson(var aJson : TJsonObject); procedure __loadGroupPermissionJson(var aJson : TJsonObject); procedure __loadUserKeysJson(var aJson : TJsonObject); public constructor Create; destructor Destroy; override; class function New : iSafety4D; function Validation : iSafety4DValidation; function resources : iSafety4DResources; function groupPermission : iSafety4DGroup; function userKey : iSafety4DUserKey; function getConfig (var aJson : TJsonObject ) : iSafety4D; function SaveToStorage ( aPath : String = '' ) : iSafety4D; function LoadConfig ( aJson : TJsonObject ) : iSafety4D; end; var vSafety4D : iSafety4D; implementation uses Safety4D.Resources.Register, Safety4D.Resources, Safety4D.Groups, Safety4D.UserKey, System.SysUtils, System.Classes, Rest.Json, Safety4D.Validation; { TSafety4D } constructor TSafety4D.Create; begin end; destructor TSafety4D.Destroy; begin inherited; end; function TSafety4D.getConfig(var aJson: TJsonObject): iSafety4D; var aResource, aGroup, aUser : TJsonObject; begin Result := Self; aJson .AddPair('resources', TJSONObject.Create) .AddPair('groupPermission', TJSONObject.Create) .AddPair('userKeys', TJSONObject.Create); aResource := aJson.GetValue('resources'); aGroup := aJson.GetValue('groupPermission'); aUser := aJson.GetValue('userKeys'); FResources.getResource(aResource); FGroupPermission.getGroups(aGroup); FuserKey.getUserKey(aUser); end; function TSafety4D.groupPermission: iSafety4DGroup; begin if not Assigned(FGroupPermission) then FGroupPermission := TSafety4DGroups.New(self); Result := FGroupPermission; end; function TSafety4D.LoadConfig(aJson: TJsonObject): iSafety4D; var aJsonResources : TJsonObject; aJsonGroupPermission : TJsonObject; aJsonUserKeys : TJsonObject; aTeste: string; begin aTeste := aJson.ToString; if aJson.TryGetValue('resources', aJsonResources) then __loadResourcesJson(aJsonResources); if aJson.TryGetValue('groupPermission', aJsonGroupPermission) then __loadGroupPermissionJson(aJsonGroupPermission); if aJson.TryGetValue('userKeys', aJsonUserKeys) then __loadUserKeysJson(aJsonUserKeys); end; class function TSafety4D.New: iSafety4D; begin if not Assigned(vSafety4D) then vSafety4D := Self.Create; Result := vSafety4D; end; function TSafety4D.resources: iSafety4DResources; begin if not Assigned(FResources) then FResources := TSafety4DResources.New(Self); Result := FResources; end; function TSafety4D.SaveToStorage(aPath: String): iSafety4D; var aJsonResult : TJsonObject; StrList : TStringList; begin if Trim(aPath) = '' then aPath := ChangeFileExt(ParamStr(0), '.s4d'); aJsonResult := TJsonObject.Create; StrList := TStringList.Create; try Self.getConfig(aJsonResult); StrList.Add(TJson.Format(aJsonResult)); StrList.SaveToFile(aPath); finally StrList.DisposeOf; aJsonResult.DisposeOf; end; end; function TSafety4D.userKey: iSafety4DUserKey; begin if not assigned(FuserKey) then FuserKey := TSafety4DUserKey.New(Self); Result := FuserKey; end; function TSafety4D.Validation: iSafety4DValidation; begin if not Assigned(FValidation) then FValidation := TSafety4DValidation.New(Self); Result := FValidation; end; procedure TSafety4D.__loadGroupPermissionJson(var aJson: TJsonObject); var I : Integer; FGroupName : String; aJsonGroup : TJsonObject; iAction : iSafety4DGroupActions; iNotAction : iSafety4DGroupNotActions; aJsonActions : TJsonArray; aJsonNotActions : TJsonArray; X: Integer; begin for I := 0 to Pred(aJson.Count) do begin FGroupName := aJson.Get(I).JsonString.Value; aJsonGroup := aJson.Get(I).JsonValue as TJsonObject; aJsonActions := aJsonGroup.GetValue('Actions'); aJsonNotActions := aJsonGroup.GetValue('NotActions'); iAction := groupPermission .groupRegister .add(FGroupName) .description(aJsonGroup.GetValue('description')) .key(aJsonGroup.GetValue('key')) .actions; for X := 0 to Pred(aJsonActions.Count) do begin iAction.add(aJsonActions.Get(X).Value); end; iNotAction := iAction.&end.notActions; for X := 0 to Pred(aJsonNotActions.Count) do begin iNotAction.add(aJsonNotActions.Get(X).Value); end; end; end; procedure TSafety4D.__loadResourcesJson(var aJson : TJsonObject); var I: Integer; FGroupName: string; FProviderName : String; aJsonGroup : TJsonObject; aJsonProvider : TJsonObject; aJsonActions : TJsonObject; J: Integer; iProviderAction : iSafety4DResourcesGroupProviderActions; K: Integer; begin for I := 0 to Pred(aJson.Count) do begin FGroupName := aJson.Get(I).JsonString.Value; aJsonGroup := aJson.Get(I).JsonValue as TJSONObject; for J := 0 to Pred(aJsonGroup.Count) do begin FProviderName := aJsonGroup.Get(J).JsonString.Value; aJsonProvider := aJsonGroup.Get(I).JsonValue as TJSONObject; aJsonActions := aJsonProvider.GetValue('actions'); iProviderAction := resources .registerResources .resourcesGroupName .add(FGroupName) .providerName .add(FProviderName) .actions; for K := 0 to Pred(aJsonActions.Count) do begin iProviderAction .add(aJsonActions.Get(K).JsonString.Value) .description(aJsonActions.Get(K).JsonValue.GetValue('description')) .errormsg(aJsonActions.Get(K).JsonValue.GetValue('errormsg')) end; end; end; end; procedure TSafety4D.__loadUserKeysJson(var aJson: TJsonObject); var I : Integer; FGroupName : String; aJsonGroup : TJsonObject; aJsonPermission : TJsonArray; X: Integer; iPermissionGroup : iSafety4DUserKeyRegisterGroupPermission; begin for I := 0 to Pred(aJson.Count) do begin FGroupName := aJson.Get(I).JsonString.Value; aJsonGroup := aJson.Get(I).JsonValue as TJsonObject; aJsonPermission := aJsonGroup.GetValue('permissionGroups'); iPermissionGroup := userKey .registerUserKey .add(FGroupName) .description(aJsonGroup.GetValue('description')); for X := 0 to Pred(aJsonPermission.Count) do begin iPermissionGroup.addPermission(aJsonPermission.Get(X).Value); end; end; end; end.