问题描述
我使用带有补丁1,2和3的Delphi RAD Server 10.4,我创建了端点来使用身份验证和安全组件TBackEndAuth和TBackendUser组件来管理RAD用户。
我可以使用这些组件成功注册新用户并登录,现在我正在尝试更新用户信息,但是在更新字段用户名时无法管理错误。
请参见代码中的方法 UpdateUser 。运行时,会引发错误:
{ “错误”:“错误”, “ 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',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内置用户端点中更新用户信息的正确方法吗?
解决方法
我不认为可以更新用户名,而只能更新“自定义字段”:
http://docwiki.embarcadero.com/RADStudio/Sydney/en/RAD_Server_Users_Resource#UpdateUser_Endpoint