angular - Jhipster:使用户能够创建自动链接到其帐户的对象
问题描述
我正在构建一项服务来帮助人们做出正确的决定。作为其中的一部分,我需要引导用户完成一个入职流程,在该流程中他们创建描述其情况的实体。
当他们输入数据时,我希望能够将这些实体相互链接,并将实体链接到用户。
为了促进这一点,我调查了以下答案:
尽管我的方法相似,但这些还没有回答我的问题。
我创建了一个用户元数据对象并通过 JDL 将其链接到 Jhipster 用户实体 - 现在我需要找到一种方法来获取登录用户,以便我可以将对该登录用户的引用添加到他们创建的实体.
我已经完成了大部分工作,并从 Jhipster 代码本身获得了指导。
我在组件中有一个方法,我有很多问题。
company: Company;
project: Project;
team: Team;
account: Account;
user: User;
userMetadata: UserMetadata;
linkEntities() {
// Question 1: Is this the right approach?
// Find the logged in account, the user is linked to this
this.accountService.identity().then(account => {
this.account = account;
});
// Find the user for that account
this.userService.find(this.account.login)
.subscribe(res => this.user = res.body);
// Find the metadata
this.userMetadataService.find(this.user.id)
.subscribe(res => this.userMetadata = res.body);
// Question 2: These values are undefined
// Is there something obvious I am missing that might explain why?
console.log(this.account);
console.log(this.user);
console.log(this.userMetadata);
// The company, project and team entities have been
// created and submitted in a previous function,
// here I update them with the references to one another
this.company.employees.push(currentlyLoggedInUserMetadata)
this.project.participants.push(currentlyLoggedInUserMetadata)
this.team.members.push(currentlyLoggedInUserMetadata)
this.company.teamOwners.push(this.team);
this.company.companyProjects.push(this.project);
this.project.implementingTeams.push(this.team);
this.project.parentCompany = this.company;
this.team.parentCompany = this.company;
this.team.teamProjects.push(this.project);
// And then send the updated entities to the api
this.subscribeToCompanySaveResponse(this.companyService.update(this.company));
this.subscribeToProjectSaveResponse(this.projectService.update(this.project));
this.subscribeToTeamSaveResponse(this.teamService.update(this.team));
}
我不知道为什么上面的三个 console.logs 有错误。我刚刚在console.logs 上方设置了这三个值。我对 RxJS 相当陌生 - observables 的工作方式是否有可能导致这种情况?
理想情况下,我希望在用户服务中保存登录用户的全局可用值(帐户服务中有一个帐户的私有实例,但没有用户 - 我应该只公开该帐户,默认情况下它是私有的?)
我不确定最佳方法或最“jhipster”方法是为当前登录的用户获取一对一链接用户<--> userMetadata 对象。
我也很清楚这是一个尝试做很多事情的大方法。一旦我可以确认整个事情都有效,我将对其进行重构以使其更易于管理。
如果有人对这种方法有建议,做过类似的事情或知道为什么帐户和用户变量特别是未定义的(我在以管理员身份登录时运行了该方法),我将不胜感激!
提前感谢您的任何时间和建议!
解决方案
JHipster 提供了一个名为 SecurityUtils 的实用程序类。您可以使用它来访问当前用户的登录名或他们的 JWT 令牌。
SecurityUtils.getCurrentUserLogin();
SecurityUtils.getCurrentUserJWT();
如果出于您的目的需要其他信息,您可以使用 UserRepository 使用使用 SecurityUtils 类检索的用户登录来检索整个用户对象。
UserRepository.findOneByLogin('usersLogin');
这存在于 API 端,因此无需在前端询问帐户对象以了解您想要实现的目标。
Ps 您无法在上面的代码中控制台记录帐户信息的原因是因为尚未解决检索该信息的承诺 - 因此,该对象仍然为空。您必须将您的 console.log 放在您的承诺解决方案中,即
this.accountService.identity().then(account => {
this.account = account;
console.log(this.account);
});
其他请求也需要相同的方法。
推荐阅读
- android - 在 Android 上手动启用蓝牙 PAN
- ruby-on-rails - Rubocop 禁用自定义警察的缓存
- visual-studio - 如何让 Visual Studio 中的 Intel Parallel 运行 .f77、.f95 等?
- java - 无法托管 MP4 com.sun HttpServer
- python-3.x - ffmpeg中的自定义颜色空间转换?
- python-3.x - 在 Python 中按色调和类别自定义颜色
- java - 如何使用 maven 和 java 将一个 google .proto 导入另一个
- angular - 为什么引导程序没有在我的新 Angular 应用程序中运行
- python - 使用分页抓取表格数据
- php - 为什么我在 PHP 中的第一个会话没有向我的第二个会话发送数据?