|
| 1 | +using System.Security.Claims; |
1 | 2 | using Microsoft.AspNetCore.Mvc; |
2 | 3 | using Microsoft.EntityFrameworkCore; |
3 | 4 | using OpenDeepWiki.EFCore; |
4 | 5 | using OpenDeepWiki.Entities; |
5 | 6 | using OpenDeepWiki.Models; |
6 | 7 | using OpenDeepWiki.Services.Auth; |
| 8 | +using OpenDeepWiki.Services.GitHub; |
| 9 | +using OpenDeepWiki.Services.Organizations; |
7 | 10 |
|
8 | 11 | namespace OpenDeepWiki.Services.Repositories; |
9 | 12 |
|
10 | 13 | [MiniApi(Route = "/api/v1/repositories")] |
11 | 14 | [Tags("仓库")] |
12 | | -public class RepositoryService(IContext context, IGitPlatformService gitPlatformService, IUserContext userContext) |
| 15 | +public class RepositoryService(IContext context, IGitPlatformService gitPlatformService, IUserContext userContext, IGitHubAppService gitHubAppService, IOrganizationService organizationService) |
13 | 16 | { |
14 | 17 | [HttpPost("/submit")] |
15 | 18 | public async Task<Repository> SubmitAsync([FromBody] RepositorySubmitRequest request) |
@@ -147,7 +150,7 @@ public async Task<RepositoryListResponse> GetListAsync( |
147 | 150 |
|
148 | 151 | if (!string.IsNullOrWhiteSpace(ownerId)) |
149 | 152 | { |
150 | | - query = query.Where(r => r.OwnerUserId == ownerId); |
| 153 | + query = query.Where(r => r.OwnerUserId == ownerId && !r.IsDepartmentOwned); |
151 | 154 | } |
152 | 155 |
|
153 | 156 | if (status.HasValue) |
@@ -263,13 +266,31 @@ public async Task<IResult> UpdateVisibilityAsync([FromBody] UpdateVisibilityRequ |
263 | 266 | // 验证所有权 |
264 | 267 | if (repository.OwnerUserId != currentUserId) |
265 | 268 | { |
266 | | - return Results.Json(new UpdateVisibilityResponse |
| 269 | + // Allow admin to manage department-owned repos in their departments |
| 270 | + var allowed = false; |
| 271 | + if (repository.IsDepartmentOwned) |
267 | 272 | { |
268 | | - Id = request.RepositoryId, |
269 | | - IsPublic = repository.IsPublic, |
270 | | - Success = false, |
271 | | - ErrorMessage = "无权限修改此仓库" |
272 | | - }, statusCode: StatusCodes.Status403Forbidden); |
| 273 | + var isAdmin = userContext.User?.IsInRole("Admin") == true; |
| 274 | + if (isAdmin) |
| 275 | + { |
| 276 | + var deptRepos = await organizationService.GetDepartmentRepositoriesAsync(currentUserId, includeRestricted: true); |
| 277 | + if (deptRepos.Any(r => r.RepositoryId == repository.Id)) |
| 278 | + { |
| 279 | + allowed = true; |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + if (!allowed) |
| 285 | + { |
| 286 | + return Results.Json(new UpdateVisibilityResponse |
| 287 | + { |
| 288 | + Id = request.RepositoryId, |
| 289 | + IsPublic = repository.IsPublic, |
| 290 | + Success = false, |
| 291 | + ErrorMessage = "无权限修改此仓库" |
| 292 | + }, statusCode: StatusCodes.Status403Forbidden); |
| 293 | + } |
273 | 294 | } |
274 | 295 |
|
275 | 296 | // 无密码仓库不能设为私有 |
@@ -356,11 +377,29 @@ public async Task<RegenerateResponse> RegenerateAsync([FromBody] RegenerateReque |
356 | 377 | // 验证所有权 |
357 | 378 | if (repository.OwnerUserId != currentUserId) |
358 | 379 | { |
359 | | - return new RegenerateResponse |
| 380 | + // Allow admin to manage department-owned repos in their departments |
| 381 | + var allowed = false; |
| 382 | + if (repository.IsDepartmentOwned) |
360 | 383 | { |
361 | | - Success = false, |
362 | | - ErrorMessage = "无权限操作此仓库" |
363 | | - }; |
| 384 | + var isAdmin = userContext.User?.IsInRole("Admin") == true; |
| 385 | + if (isAdmin) |
| 386 | + { |
| 387 | + var deptRepos = await organizationService.GetDepartmentRepositoriesAsync(currentUserId, includeRestricted: true); |
| 388 | + if (deptRepos.Any(r => r.RepositoryId == repository.Id)) |
| 389 | + { |
| 390 | + allowed = true; |
| 391 | + } |
| 392 | + } |
| 393 | + } |
| 394 | + |
| 395 | + if (!allowed) |
| 396 | + { |
| 397 | + return new RegenerateResponse |
| 398 | + { |
| 399 | + Success = false, |
| 400 | + ErrorMessage = "无权限操作此仓库" |
| 401 | + }; |
| 402 | + } |
364 | 403 | } |
365 | 404 |
|
366 | 405 | // 只有失败或完成状态才能重新生成 |
|
0 commit comments