demousersgroups.pp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. program demousersgroups;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, jsonparser, fphttpwebclient, fpoauth2ini,
  5. restbase, odatabase, msgraph, office365client;
  6. type
  7. { TGroupsDemoApplication }
  8. TGroupsDemoApplication = class(TCustomApplication)
  9. private
  10. FSession,
  11. FLogFile,
  12. FConfig : String;
  13. FCLient : TOffice365Client;
  14. FGS : TService;
  15. FGraph : TGraphService;
  16. FMaxLevel : Integer;
  17. procedure EnsureService;
  18. procedure GetUserGroups(AUser: String);
  19. procedure GetGroupMembers(AGroup : String);
  20. procedure ShowUsers;
  21. procedure ShowGroups;
  22. protected
  23. procedure DoRun; override;
  24. public
  25. constructor Create(TheOwner: TComponent); override;
  26. destructor Destroy; override;
  27. procedure WriteHelp(Msg : String); virtual;
  28. end;
  29. procedure TGroupsDemoApplication.EnsureService;
  30. Var
  31. FIS : TFPOAuth2IniStore;
  32. begin
  33. // Auth client
  34. FCLient:=TOffice365Client.Create(Self);
  35. FClient.WebClient:=TFPHTTPWebClient.Create(Self);
  36. FClient.WebClient.LogFile:=FLogFile;
  37. FClient.AuthHandler:=TAzureADOAuth2Handler.Create(Self);
  38. FIS:=TFPOAuth2IniStore.Create(Self);
  39. FIS.ConfigFileName:=FConfig;
  40. FIS.SessionFileName:=FConfig;
  41. FClient.AuthHandler.Store:=FIS;
  42. FClient.AuthHandler.LoadConfig();
  43. FClient.AuthHandler.LoadSession(FSession);
  44. FClient.AuthHandler.Config.AuthScope:='https://graph.microsoft.com/';
  45. FClient.WebClient.RequestSigner:=FClient.AuthHandler;
  46. FClient.AuthHandler.WebClient:=FClient.WebClient;
  47. // Service
  48. FGS:=TService.Create(Self);
  49. FGS.ServiceURL:='https://graph.microsoft.com/v1.0/';
  50. FGS.APINeedsAuth:=True;
  51. FGS.WebClient:=FClient.WebClient;
  52. // Default container
  53. FGraph:=FGS.GraphService;
  54. end;
  55. procedure TGroupsDemoApplication.GetUserGroups(AUser: String);
  56. Var
  57. DOES : TdirectoryObjectsEntitySet;
  58. L : TdirectoryObjectArray;
  59. D : TdirectoryObject;
  60. U : TUser;
  61. begin
  62. if AUser='' then
  63. U:=FGraph.me
  64. else
  65. begin
  66. U:=FGraph.users.Get(AUser);
  67. U.BasePath:='/users/'+AUser;
  68. end;
  69. DOES:=U.memberOf(FGS);
  70. L:=Does.ListAll('');
  71. For D in L do
  72. begin
  73. Writeln('ID ',D.id,': ',D.additionalProperties.AsJSON);
  74. end;
  75. end;
  76. procedure TGroupsDemoApplication.GetGroupMembers(AGroup: String);
  77. Var
  78. G : TGroup;
  79. DA : TDirectoryObjectArray;
  80. D : TDirectoryObject;
  81. begin
  82. G:=FGraph.groups.get(agroup);
  83. G.BasePath:='/groups/'+G.Id;
  84. DA:=G.members(FGS).ListAll('');
  85. for D in DA do
  86. begin
  87. Writeln('Member ',D.Id,': ',D.additionalProperties.AsJSON);
  88. end;
  89. end;
  90. { TGroupsDemoApplication }
  91. procedure TGroupsDemoApplication.DoRun;
  92. var
  93. FUser,ErrorMsg: String;
  94. begin
  95. // quick check parameters
  96. ErrorMsg:=CheckOptions('hl:r:c:s:u:UGg',['help','recurse:','logfile:','config:','session:','user:','users','groups','group']);
  97. if (ErrorMsg<>'') or HasOption('h', 'help') then
  98. WriteHelp(ErrorMsg);
  99. FLogFile:=GetOptionValue('l','logfile');
  100. if FLogFile='' then
  101. FLogFile:=ExtractFilePath(ParamStr(0))+'requests.log';
  102. FConfig:=GetOptionValue('c','config');
  103. if FConfig='' then
  104. FConfig:=ExtractFilePath(ParamStr(0))+'msgraph.ini';
  105. FMaxLevel:=StrToIntDef(GetOptionValue('r','recurse'),3);
  106. FSession:=GetOptionValue('s','session');
  107. FUser:=getOptionValue('u','user');
  108. if (FSession='') then
  109. WriteHelp('Need session');
  110. If FMaxLevel<2 then
  111. FMaxLevel:=2;
  112. EnsureService;
  113. if HasOption('G','groups') then
  114. ShowGroups
  115. else if HasOption('U','users') then
  116. ShowUsers
  117. else if HasOption('g','group') then
  118. GetGroupMembers(GetOptionValue('g','group'))
  119. else
  120. GetUserGroups(FUser);
  121. Terminate;
  122. end;
  123. procedure TGroupsDemoApplication.ShowUsers;
  124. Var
  125. L : TUserArray;
  126. U : TUser;
  127. begin
  128. L:=FGraph.users.ListAll('');
  129. for U in L do
  130. begin
  131. Writeln('User ',U.Id,' : ',U.DisplayName,', givenName:',U.givenName,', email: ',U.mail);
  132. end;
  133. end;
  134. procedure TGroupsDemoApplication.ShowGroups;
  135. Var
  136. L : TGroupArray;
  137. G : TGroup;
  138. begin
  139. L:=FGraph.groups.ListAll('');
  140. for G in L do
  141. begin
  142. Writeln('Group ',G.Id,' : ',G.DisplayName,', Mail:',G.mail);
  143. end;
  144. end;
  145. constructor TGroupsDemoApplication.Create(TheOwner: TComponent);
  146. begin
  147. inherited Create(TheOwner);
  148. StopOnException:=True;
  149. end;
  150. destructor TGroupsDemoApplication.Destroy;
  151. begin
  152. inherited Destroy;
  153. end;
  154. procedure TGroupsDemoApplication.WriteHelp(Msg: String);
  155. begin
  156. If Msg<>'' then
  157. Writeln('Error : ',Msg);
  158. Writeln('Usage: ', ExeName, ' -s session [options]');
  159. Writeln('Where options : ');
  160. Writeln('-h --help this help');
  161. Writeln('-c --config config file with session and client data (default msgraph.ini)');
  162. Writeln('-l --logfile config file with session and client data (default requests.log)');
  163. Writeln('-s --session=name session to load from config file');
  164. Writeln('-r --recurse=level maximum recursion level (defult 2)');
  165. Halt(Ord(Msg<>''));
  166. end;
  167. var
  168. Application: TGroupsDemoApplication;
  169. begin
  170. Application:=TGroupsDemoApplication.Create(nil);
  171. Application.Title:='OneDrive Demo Command-line Application';
  172. Application.Run;
  173. Application.Free;
  174. end.