鼎鼎知识库
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

2021.3.11办公楼版用户相关逻辑.md 11KB

il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. 请求`api/auth/token`接口。
  2. 如果用户不存在,创建写字楼版的用户以及用户角色中间表。在`aspnetusers`中增加一条记录,在`aspnetuserroles`增加一条记录,角色为`OfficeGuest`。
  3. 如果用户存在,会根据手机号去`projectuserroles`中找角色为`HdDg`的记录。
  4. 如果找到,说明是总部电工(办公楼版的超级管理员)。
  5. ```
  6. dto.ProjectId = currentUser.ProjectIds;
  7. dto.ProjectName = string.Empty;
  8. ```
  9. 以上,可以看出,当原先的总部运营后台设置总部电工和项目关系时,实际上是保存在了`aspnetusers`表的`ProjectIds`字段中。而在新版的运营后台,与总部电工有关的项目要去`HdDgProject`中去找。
  10. 如果找不到,说明是办公楼版的其它角色用户。会去项目数据库的`CompanyUser`中去找当前用户的相关公司。
  11. ```
  12. var currentCompanyUsers = _companyUserRepo.GetCompanyUsersByUserIdAsync(currentUser.Id.ToString()).ToList();
  13. ```
  14. 也就是说,一个用户可以在不同的`Company`中。
  15. 以上,出现了`aspnetusers`, `aspnetuserroles`,``projectuserroles`,`companyuser` 这4张表。
  16. ```
  17. aspnetusers
  18. Id
  19. UserName
  20. ProjectIds
  21. aspnetuserroles
  22. UserId
  23. RoleId
  24. projectuserroles
  25. UserId
  26. RoleId
  27. ProjectId
  28. CompanyId
  29. companyuser
  30. CompanyId
  31. UserId
  32. RoleId
  33. RoleName
  34. IsAllowed: 是否允许新朋友加入
  35. HasRole:是否具有角色
  36. ```
  37. 如果是手机端的用户:`13800000000`
  38. ```
  39. aspnetusers
  40. Id:1
  41. UserName:13800000000
  42. ProjectIds
  43. aspnetuserroles
  44. UserId:1
  45. RoleId:角色为OfficeGuest的aspnetroles的主键
  46. ```
  47. 如果总部运营后台添加了一个电工:13811111111
  48. ```
  49. aspnetusers
  50. Id:2
  51. UserName:13811111111
  52. ProjectIds: 9,10
  53. aspnetuserroles
  54. UserId:2
  55. RoleId:角色为HdDg的aspnetroles的主键
  56. ```
  57. 电工`13811111111`,打开`APP`
  58. ![](F:\SourceCodes\DDWiki\专题\后端\office6.png)
  59. 电工`13811111111`,点击某个项目,展示公司列表。
  60. ![](F:\SourceCodes\DDWiki\专题\后端\office7.png)
  61. 电工`13811111111`,添加公司。
  62. ![](F:\SourceCodes\DDWiki\专题\后端\office8.png)
  63. 对应的接口在`api/super/addUpdateCompany`
  64. ```
  65. var newCompany = new Company(request.ProjectId, request.Name,request.LocationIds,request.IsAdmin, request.UserId);
  66. _companyRepo.Add(newCompany);
  67. await _companyRepo.UnitOfWork.SaveChangesAsync();
  68. var myCompanyId = newCompany.Id.ToString();//新增公司ID
  69. //获取当前用户信息,并更新相应的用户权限表
  70. var user = await _auth.GetUserByIdAsync(request.UserId);
  71. //添加用户
  72. var rolestr = await _auth.CreatOfficeAdmin(user.Mobile, request.ProjectId, myCompanyId, user.UserName);
  73. string[] roleInfo = rolestr.Split(',');
  74. //添加公司用户
  75. if (roleInfo.Length == 3)
  76. {
  77. CompanyUser newUser = new CompanyUser(myCompanyId, roleInfo[0], roleInfo[1], roleInfo[2]);
  78. _companyUser.Add(newUser);
  79. await _companyUser.UnitOfWork.SaveChangesAsync();
  80. }
  81. ```
  82. 数据库中对应的:
  83. ```
  84. aspnetusers
  85. Id:2
  86. UserName:13811111111
  87. ProjectIds: 9,10
  88. aspnetuserroles
  89. UserId:2
  90. RoleId:角色为HdDg的aspnetroles的主键
  91. projectuserroles
  92. UserId:2
  93. RoleId:角色为HdDg的aspnetroles的主键
  94. ProjectId:9
  95. CompanyId:1
  96. companyuser
  97. CompanyId:1
  98. UserId:2
  99. RoleId:角色为HdDg的aspnetroles的主键
  100. RoleName:HdDg
  101. IsAllowed: false
  102. HasRole:true
  103. ```
  104. 电工`13811111111`,点击"我的",点击项目管理。
  105. ![](F:\SourceCodes\DDWiki\专题\后端\office1.png)
  106. 展示项目列表。其实对后端开发人员来说,项目列表就是`Company`列表。
  107. ![](F:\SourceCodes\DDWiki\专题\后端\office2.png)
  108. 电工点击"邀请好友",展示二维码。目前角色为`OfficeGuest`的用户`13800000000`扫描二维码。
  109. ![](F:\SourceCodes\DDWiki\专题\后端\office3.png)
  110. 目前角色为`OfficeGuest`的用户`13800000000`点击"加入该项目"。
  111. ![](F:\SourceCodes\DDWiki\专题\后端\office4.png)
  112. 对应的接口在`api/mine/joinProject`中。
  113. ```
  114. var guestRole = await _auth.GetRoleByNameAsync(OfficeConstants.Office_Role_Guest);
  115. var ef = await _officeHelper.GetOfficeContextByProjectIdAsync(request.ProjectId);
  116. _companyUserRepo = new CompanyUserRepository(ef);
  117. //先检查这个用户是否已经在CompanyUser中有记录,因为用户可以多次扫码加入,这种情况就不在数据库中再添加记录了
  118. if (_companyUserRepo.GetCompanyUsersByUserIdAsync(request.UserId).ToList().Any(t => t.CompanyId == request.CompanyId))//已在本公司中的用户无法再加入公司
  119. result.Message = "用户已经加入该项目了";
  120. else
  121. {
  122. //管理员不能扫码加入自己的项目
  123. var AdminRole = await _auth.GetRoleByNameAsync(OfficeConstants.Office_Role_Admin);
  124. var companyAdmin = _companyUserRepo.GetUsersByCompanyId(request.CompanyId).FirstOrDefault(t => t.RoleId == AdminRole.Id);
  125. if (companyAdmin != null && companyAdmin.UserId == request.UserId)
  126. {
  127. result.Message = "管理员不能扫码加入自己管理的项目";
  128. return await Task.FromResult(result);
  129. }
  130. //var newCompanyUser = new CompanyUser(request.CompanyId, request.UserId, guestRole.Id, guestRole.Name);
  131. var newCompanyUser = new CompanyUser(request.CompanyId, request.UserId);
  132. _companyUserRepo.Add(newCompanyUser);
  133. await _companyUserRepo.UnitOfWork.SaveChangesAsync(cancellationToken);
  134. }
  135. ```
  136. 以上,排除了用户`13800000000`已经在`CompanyUser`中有记录,以及管理员不能扫码进入当前`Company`这两种情况,然后再`CompanyUser`中添加了一条记录。
  137. ```
  138. aspnetusers
  139. Id:1
  140. UserName:13800000000
  141. ProjectIds
  142. aspnetuserroles
  143. UserId:1
  144. RoleId:角色为OfficeGuest的aspnetroles的主键
  145. companyuser
  146. CompanyId:1
  147. UserId:1
  148. RoleId
  149. RoleName
  150. IsAllowed:
  151. HasRole:false
  152. ```
  153. 电工`13811111111`,点击"新朋友"。
  154. ![](F:\SourceCodes\DDWiki\专题\后端\office2.png)
  155. 展示新朋友列表。
  156. ![](F:\SourceCodes\DDWiki\专题\后端\office5.png)
  157. 对应的接口在`api/mine/newFriends`
  158. ```
  159. if (await _auth.IsOfficeSuperAdminAsync(request.UserId))//超级管理员
  160. {
  161. var superAdminRole = await _auth.GetRoleByNameAsync(OfficeConstants.Office_Role_SuperAdmin);
  162. adminCompanyIds = companyUsers.Where(t => t.UserId == request.UserId && t.RoleId == superAdminRole.Id).Select(t => t.CompanyId).ToList();
  163. }
  164. else//管理员
  165. {
  166. //获取管理员角色的主键
  167. var officeAdminRole = await _auth.GetRoleByNameAsync(OfficeConstants.Office_Role_Admin);
  168. //找出管理员所在的公司主键集合
  169. adminCompanyIds = companyUsers.Where(t => t.UserId == request.UserId && t.RoleId == officeAdminRole.Id.ToString()).Select(t => t.CompanyId).ToList();
  170. }
  171. companyUsers = companyUsers.Where(t => t.IsAllowed == false && adminCompanyIds.Any(x => x == t.CompanyId)).ToList();
  172. foreach (var companyUser in companyUsers)
  173. {
  174. dto.Items.Add(HandleItem(companyUser, request, _companyRepo));
  175. }
  176. dto.Items = dto.Items.OrderByDescending(t => DateTime.Parse(t.CreateTime)).ToList();
  177. result.data = dto;
  178. ```
  179. 点击"同意",对应的接口在:`api/mine/acceptNewFriend`
  180. ```
  181. if (!_companyUserRepo.HasCompanyRole(request.ObjUserId, request.CompanyId, guestRole.Name))//保证多次扫码只加一次
  182. {
  183. var companyUsers = _companyUserRepo.GetAll();
  184. var currentCompanyUser = companyUsers.FirstOrDefault(t => t.CompanyId == request.CompanyId && t.UserId == request.ObjUserId);//肯定存在
  185. currentCompanyUser.AcceptNewFriendNew(guestRole.Id,guestRole.Name);
  186. _companyUserRepo.Update(currentCompanyUser);
  187. await _companyUserRepo.UnitOfWork.SaveChangesAsync(cancellationToken);
  188. //TODO:需要保证事务的原子性、持久性、隔离性
  189. //获取总部用户设置ProjectId
  190. await _auth.SetProjectIdForUserAsync(request.ObjUserId, request.ProjectId);
  191. await _auth.AddProjectUserRoleAsync(request.UserId, guestRole.Id, request.ProjectId, request.CompanyId);
  192. }
  193. ```
  194. 数据库方面
  195. ```
  196. aspnetusers
  197. Id:1
  198. UserName:13800000000
  199. ProjectIds:9
  200. aspnetuserroles
  201. UserId:1
  202. RoleId:角色为OfficeGuest的aspnetroles的主键
  203. companyuser
  204. CompanyId:1
  205. UserId:1
  206. RoleId:角色为OfficeGuest的aspnetuserroles主键
  207. RoleName:OfficeGuest
  208. IsAllowed: true
  209. HasRole:true
  210. projectuserroles
  211. UserId:1
  212. RoleId:角色为OfficeGuest的aspnetroles的主键
  213. ProjectId:9
  214. CompanyId:1
  215. ```
  216. 启用"设置为管理员"
  217. ![](F:\SourceCodes\DDWiki\专题\后端\office9.png)
  218. 点击`确定`按钮。对应的接口在:`api/mine/setUserRole`中。
  219. ```
  220. var ef = await _dbHelper.GetOfficeContextByProjectIdAsync(request.ProjectId);
  221. _companyUserRepo = new CompanyUserRepository(ef);
  222. var currentCompanyUser = _companyUserRepo.GetAll().FirstOrDefault(t => t.UserId == request.ObjectUserId && t.CompanyId == request.CompanyId);
  223. var currentRole = await _auth.GetRoleByNameAsync(request.RoleName);
  224. if(await _auth.IsOfficeSuperAdminAsync(request.UserId)|| request.RoleName != OfficeConstants.Office_Role_Admin)//超级管理员
  225. {
  226. currentCompanyUser.ChangeRole(currentRole.Id, currentRole.Name);
  227. _companyUserRepo.Update(currentCompanyUser);
  228. await _companyUserRepo.UnitOfWork.SaveChangesAsync(cancellationToken);
  229. await _auth.UpdateProjectUserRoleByRoleNameAsync(currentRole.Id, request.ObjectUserId, request.ProjectId, request.CompanyId);
  230. }
  231. ```
  232. 数据库方面
  233. ```
  234. aspnetusers
  235. Id:1
  236. UserName:13800000000
  237. ProjectIds:9
  238. aspnetuserroles(似乎后来没用)
  239. UserId:1
  240. RoleId:角色为OfficeGuest的aspnetroles的主键
  241. companyuser
  242. CompanyId:1
  243. UserId:1
  244. RoleId:角色为OfficeAdmin的aspnetuserroles主键
  245. RoleName:OfficeAdmin
  246. IsAllowed: true
  247. HasRole:true
  248. projectuserroles
  249. UserId:1
  250. RoleId:角色为OfficeAdmin的aspnetuserroles主键
  251. ProjectId:9
  252. CompanyId:1
  253. ```
  254. 至此,总部电工为项目下的某个Company设置Company管理员成功。
  255. 另外还有
  256. - Company的管理员设置新的管理员,
  257. - Company的管理员设置操作员