0

我使用带有补丁 1,2 和 3 的 Delphi RAD Server 10.4,我创建了端点来使用身份验证和安全组件 TBackEndAuth 和 TBackendUser 组件来管理 RAD 用户。

我可以使用这些组件成功注册一个新用户并登录,现在我正在尝试更新用户信息,但在更新字段username时我无法管理错误。

在代码中查看方法UpdateUser。运行时,它会引发错误:

{ "error": "Error", "description": "EMS 错误:错误请求。操作无法完成,因为一个或多个动态名称与静态名称冲突。冲突:用户名" }

如果我注释行lobjUser.AddPair('username',lusername_new); 然后它工作正常。但我也希望能够更新用户名。

非常欢迎任何帮助。谢了。

    unit DMUsermanager;

// EMS Resource Module

interface

uses
  System.SysUtils, System.Classes, System.JSON,
  EMS.Services, EMS.ResourceAPI, EMS.ResourceTypes, REST.Backend.ServiceTypes,
  REST.Backend.MetaTypes, REST.Backend.EMSServices, REST.Backend.Providers,
  REST.Backend.ServiceComponents, Data.Bind.Components, Data.Bind.ObjectScope,
  REST.Backend.BindSource, REST.Backend.EMSProvider;

type
  [ResourceName('UserManager')]
  TUsermanagerResource1 = class(TDataModule)
    EMSProvider1  : TEMSProvider;
    BackendAuth1  : TBackendAuth;
    BackendUsers1 : TBackendUsers;
  published

    [EndpointMethod(TEndpointRequest.TMethod.Post)]
    [ResourceSuffix('/SignUpUser')]
    procedure SignUpUser(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);

    [EndpointMethod(TEndpointRequest.TMethod.Post)]
    [ResourceSuffix('/LoginUser')]
    procedure LoginUser(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);


    [EndpointMethod(TEndpointRequest.TMethod.Post)]
    [ResourceSuffix('/UpdateUser')]
    procedure UpdateUser(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);

  end;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

procedure TUsermanagerResource1.SignUpUser(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
   lusername, lpassword                 : string;
   lcustomfieldemail, lcustomfieldage   : string;
   lcustomfields                        : TCollectionItem;
   jo                                   : TJSONObject;
begin
     if ARequest.Body.TryGetObject(jo) then
     begin
           // Get user data from JSON post request body
           lusername         := jo.GetValue<string>('username');
           lpassword         := jo.GetValue<string>('password');
           lcustomfieldemail := jo.GetValue<string>('email');
           lcustomfieldage   := jo.GetValue<string>('age');

           with BackEndAuth1 do
           begin
                 //Create CustomFields collection
                 UserDetails.Add;
                 UserDetails.Items[0].Name  := 'email';
                 UserDetails.Items[0].Value := lcustomfieldemail;

                 UserDetails.Add;
                 UserDetails.Items[1].Name  := 'age';
                 UserDetails.Items[1].Value := lcustomfieldage;

                 //Assign username and passowrd
                 UserName := lusername;
                 Password := lpassword;

                 //Creates the user in RAD Server
                 Signup;

                 // Check if creation was Ok and return user info
                 if LoggedIn then
                 begin
                      jo := TJSONObject.Create;
                      jo.AddPair('Username',LoggedInUserName);
                      jo.AddPair('Token'   ,LoggedInToken);
                      jo.AddPair('Email'   ,UserDetails[0].value);
                      jo.AddPair('Age'     ,UserDetails[1].value);
                      jo.AddPair('ID'      ,LoggedInValue.ObjectID);
                      AResponse.Body.SetValue(jo,true);
                 end
                 else
                     AResponse.RaiseBadRequest('Error on SignUp the user.');
           end;
     end
     else
         // JSON object content or format error
         AResponse.RaiseBadRequest('Error on request body JSONOBJECT.');
end;


procedure TUsermanagerResource1.LoginUser(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
   lusername, lpassword                 : string;
   jo                                   : TJSONObject;
begin
     if ARequest.Body.TryGetObject(jo) then
     begin
           // Get user credentials from JSON post request body
           lusername := jo.GetValue<string>('username');
           lpassword := jo.GetValue<string>('password');

           with BackEndAuth1 do
           begin
                 //Create CustomFields collection
                 UserDetails.Add;
                 UserDetails.Items[0].Name  := 'email';

                 UserDetails.Add;
                 UserDetails.Items[1].Name  := 'age';

                 //Assign username and passowrd
                 UserName := lusername;
                 Password := lpassword;

                 //Log in into RAD Server
                 Login;

                 // Check if Log in was Ok and return user info
                 if LoggedIn then
                 begin
                      jo := TJSONObject.Create;
                      jo.AddPair('Username',LoggedInUserName);
                      jo.AddPair('Token'   ,LoggedInToken);
                      jo.AddPair('Email'   ,UserDetails[0].value);
                      jo.AddPair('Age'     ,UserDetails[1].value);
                      jo.AddPair('ID'      ,LoggedInValue.ObjectID);
                      AResponse.Body.SetValue(jo,true);
                 end
                 else
                     AResponse.RaiseBadRequest('Error on Log in the user.');
           end;
     end
     else
         // JSON object content or format error
         AResponse.RaiseBadRequest('Error on request body JSONOBJECT.');
end;

procedure TUsermanagerResource1.UpdateUser(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
   lusername, lpassword                 : string;
   lusername_new, lpassword_new         : string;
   lcustomfieldemail, lcustomfieldage   : string;
   lcustomfields                        : TCollectionItem;
   jo, lobjUser                         : TJSONObject;
   lUpdatedAt                           : TBackendEntityValue;
begin
     if ARequest.Body.TryGetObject(jo) then
     begin
           // Get user data from JSON post request body
           lusername         := jo.GetValue<string>('username');
           lpassword         := jo.GetValue<string>('password');
           lusername_new     := jo.GetValue<string>('username_new');
           lpassword_new     := jo.GetValue<string>('password_new');
           lcustomfieldemail := jo.GetValue<string>('email');
           lcustomfieldage   := jo.GetValue<string>('age');

           with BackEndAuth1 do
           begin
                 //Assign username and passowrd
                 UserName := lusername;
                 Password := lpassword;

                 //Log in into RAD Server
                 Login;

                 if LoggedIn then
                 begin
                      //Create CustomFields collection
                      UserDetails.Add;
                      UserDetails.Items[0].Name  := 'email';
                      UserDetails.Items[0].Value := lcustomfieldemail;

                      UserDetails.Add;
                      UserDetails.Items[1].Name  := 'age';
                      UserDetails.Items[1].Value := lcustomfieldage;

                     // UserName := lusername_new;  ==> this does not work
                     // Password := lpassword_new;  ==> this does not work

                      //Updates user data in RAD Server
                      UpdateuserDetails;

                      lobjUser := TJSONObject.Create;
                      lobjUser.AddPair('username',lusername_new);  // this line is the ISSUE !!
                      lobjUser.AddPair('password',lpassword_new);

                      BackEndUsers1.Users.UpdateUser(LoggedInValue.ObjectID,lobjuser,lUpdatedAt);

                     // Check if creation was Ok and return user info
                     if LoggedIn then
                     begin
                          jo := TJSONObject.Create;
                          jo.AddPair('Username',LoggedInUserName);
                          jo.AddPair('Token'   ,LoggedInToken);
                          jo.AddPair('Email'   ,UserDetails[0].value);
                          jo.AddPair('Age'     ,UserDetails[1].value);
                          jo.AddPair('ID'      ,LoggedInValue.ObjectID);
                          AResponse.Body.SetValue(jo,true);
                     end
                     else
                         AResponse.RaiseBadRequest('Error on SignUp the user.');
                 end;
           end;
     end
     else
         // JSON object content or format error
         AResponse.RaiseBadRequest('Error on request body JSONOBJECT.');
end;


procedure Register;
begin
  RegisterResource(TypeInfo(TUsermanagerResource1));
end;

initialization
  Register;
end.

还有一个问题:这是在 RAD Sever 内置用户端点中更新用户信息的正确方法吗?

4

1 回答 1

1

我认为不能更新用户名,而只能更新自定义字段:

http://docwiki.embarcadero.com/RADStudio/Sydney/en/RAD_Server_Users_Resource#UpdateUser_Endpoint

于 2020-10-04T21:14:38.467 回答