|
| 1 | +/*************************************************************/ |
| 2 | +/* Copyright (c) 2024 by progress Software Corporation */ |
| 3 | +/* */ |
| 4 | +/* all rights reserved. no part of this program or document */ |
| 5 | +/* may be reproduced in any form or by any means without */ |
| 6 | +/* permission in writing from progress Software Corporation. */ |
| 7 | +/*************************************************************/ |
| 8 | +/*------------------------------------------------------------------------ |
| 9 | + File : get_user.p |
| 10 | + Purpose : |
| 11 | + Syntax : |
| 12 | + Description : Fetch the user & granted roles |
| 13 | + Author(s) : tmasood |
| 14 | + Created : Thur Aug 08 15:29:10 IST 2024 |
| 15 | + Notes : |
| 16 | + ----------------------------------------------------------------------*/ |
| 17 | + |
| 18 | +routine-level on error undo, throw. |
| 19 | + |
| 20 | +using Progress.Lang.Error. |
| 21 | +using OpenEdge.DataAdmin.DataAdminService. |
| 22 | +using OpenEdge.DataAdmin.Rest.IRestRequest. |
| 23 | +using OpenEdge.DataAdmin.Error.DataAdminErrorHandler. |
| 24 | +using OpenEdge.DataAdmin.IUser. |
| 25 | +using OpenEdge.DataAdmin.IUserSet. |
| 26 | +using OpenEdge.DataAdmin.Error.NotFoundError. |
| 27 | +using OpenEdge.DataAdmin.Rest.IPageRequest. |
| 28 | +using OpenEdge.DataAdmin.IGrantedRoleSet. |
| 29 | +using OpenEdge.DataAdmin.IGrantedRole. |
| 30 | +using OpenEdge.DataAdmin.IRoleSet. |
| 31 | +using OpenEdge.DataAdmin.Lang.Collections.IIterator. |
| 32 | +using Progress.Json.ObjectModel.JsonObject. |
| 33 | +using Progress.Json.ObjectModel.JsonArray. |
| 34 | + |
| 35 | +/* old behavior - to be deprecated */ |
| 36 | +{darest/restbase.i get user} |
| 37 | + |
| 38 | +procedure Execute: |
| 39 | + define input parameter restRequest as IRestRequest no-undo. |
| 40 | + |
| 41 | + /* *************************** Definitions ************************** */ |
| 42 | + define variable oUser as IUser no-undo. |
| 43 | + define variable userset as IUserSet no-undo. |
| 44 | + define variable pageRequest as IPageRequest no-undo. |
| 45 | + define variable service as DataAdminService no-undo. |
| 46 | + define variable errorHandler as DataAdminErrorHandler no-undo. |
| 47 | + define variable oGrants as IGrantedRoleSet no-undo. |
| 48 | + define variable oGrant as IGrantedRole no-undo. |
| 49 | + define variable iter as IIterator no-undo. |
| 50 | + define variable cUserID as character no-undo. |
| 51 | + define variable oUserJson as JsonObject no-undo. |
| 52 | + define variable oRoleArr as JsonArray no-undo. |
| 53 | + |
| 54 | + /* *************************** Main Block *************************** */ |
| 55 | + |
| 56 | + restRequest:Validate(). |
| 57 | + service = new DataAdminService(restRequest:ConnectionName). |
| 58 | + service:URL = restRequest:ConnectionUrl. |
| 59 | + cUserID = restRequest:KeyValue[1]. |
| 60 | + |
| 61 | + if (cUserID gt "") eq true then |
| 62 | + do: |
| 63 | + // Obtain the single user record for the ID given. |
| 64 | + oUser = service:GetUser(cUserID). |
| 65 | + if not valid-object(oUser) then |
| 66 | + undo, throw new NotFoundError("User '" + cUserID + "' not found"). |
| 67 | + oUserJson = oUser:ExportToJson(). |
| 68 | + |
| 69 | + // Add a new "roles" property as array to the single user object returned. |
| 70 | + oUserJson:GetJsonArray("users"):GetJsonObject(1):Add("roles", new JsonArray()). |
| 71 | + |
| 72 | + // Iterate through the roles granted to this user@domain as the grantee. |
| 73 | + oGrants = service:GetGrantedRoles(substitute("where Grantee eq '&1@&2'", oUser:Name, oUser:Domain:Name)). |
| 74 | + |
| 75 | + iter = oGrants:Iterator(). |
| 76 | + do while iter:HasNext(): |
| 77 | + assign oGrant = cast(iter:Next(), IGrantedRole). |
| 78 | + // Add the individual array element found in each of the Grant objects within the set. |
| 79 | + oUserJson:GetJsonArray("users"):GetJsonObject(1):GetJsonArray("roles"):Add(oGrant:Role:ExportToJson():GetJsonArray("roles"):GetJsonObject(1)). |
| 80 | + end. |
| 81 | + end. |
| 82 | + else |
| 83 | + oUserJson = new JsonObject(). |
| 84 | + |
| 85 | + oUserJson:WriteFile(restRequest:OutFileName, true). |
| 86 | + |
| 87 | + catch e as Progress.Lang.Error: |
| 88 | + if session:batch-mode then |
| 89 | + errorHandler = new DataAdminErrorHandler(restRequest:ErrorFileName). |
| 90 | + else do: |
| 91 | + errorHandler = new DataAdminErrorHandler(). |
| 92 | + end. |
| 93 | + errorHandler:Error(e). |
| 94 | + end catch. |
| 95 | + finally: |
| 96 | + delete object service no-error. |
| 97 | + end finally. |
| 98 | +end. |
0 commit comments