Files
flatrender/services/identity/FlatRender.IdentitySvc/Controllers/AdminController.cs
T
soroush.asadi 675b60d858
Build backend images / build content-svc (push) Failing after 1m2s
Build backend images / build file-svc (push) Failing after 3m11s
Build backend images / build gateway (push) Failing after 5m39s
Build backend images / build identity-svc (push) Failing after 38s
Build backend images / build notification-svc (push) Failing after 2m0s
Build backend images / build render-svc (push) Failing after 58s
Build backend images / build studio-svc (push) Failing after 58s
feat(auth+admin): Sign in with Google (OAuth) + Integrations config panel
Backend (identity-svc):
- oauth_config table (mig 22) + OAuthConfig entity
- OAuthService: admin config CRUD + Google authorization-code flow (build consent
  URL, exchange code, fetch userinfo, find/create RegisterMode.Google user, issue
  session via AuthService.IssueOAuthSessionAsync)
- AuthController: GET /v1/auth/google/{start,callback} (public); tokens handed to
  frontend via URL fragment
- AdminController: GET/PUT /v1/admin/oauth/{provider} (admin, secret masked)

Frontend:
- "ورود با گوگل" button on /auth → identity start endpoint
- /auth/callback reads fragment tokens → /api/auth/oauth-session sets httpOnly cookies
- /admin/integrations: Google client_id/secret/redirect_uri + enable, with setup guide
- nav + fa/en labels

Client ID/Secret are configured entirely in the admin panel — no redeploy needed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 00:08:21 +03:30

96 lines
4.4 KiB
C#

using FlatRender.IdentitySvc.Application.Services;
using FlatRender.IdentitySvc.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FlatRender.IdentitySvc.Controllers;
[ApiController]
[Authorize(Roles = "Admin")]
public class AdminController(AdminService svc) : ControllerBase
{
private Guid TenantId =>
Guid.TryParse(User.FindFirst("tenant_id")?.Value, out var t)
? t : Guid.Parse("00000000-0000-0000-0000-000000000001");
// ── CRM analytics ────────────────────────────────────────────────────────
[HttpGet("v1/admin/crm/analytics")]
public async Task<IActionResult> Crm([FromQuery] DateTime? start, [FromQuery] DateTime? end)
{
var s = start ?? DateTime.UtcNow.AddDays(-30);
var e = end ?? DateTime.UtcNow;
return Ok(await svc.GetCrmAnalyticsAsync(TenantId, s, e));
}
// ── Plan statistics ──────────────────────────────────────────────────────
[HttpGet("v1/admin/plan-statistics")]
public async Task<IActionResult> PlanStats() => Ok(await svc.GetPlanStatisticsAsync(TenantId));
// ── OAuth provider config ──────────────────────────────────────────────────
[HttpGet("v1/admin/oauth/{provider}")]
public async Task<IActionResult> GetOAuth(string provider, [FromServices] OAuthService oauth)
{
var c = await oauth.GetConfigAsync(provider);
return Ok(new OAuthConfigResponse(provider, c?.ClientId, c?.RedirectUri, c?.Enabled ?? false,
!string.IsNullOrEmpty(c?.ClientSecret)));
}
[HttpPut("v1/admin/oauth/{provider}")]
public async Task<IActionResult> PutOAuth(string provider, [FromBody] UpsertOAuthConfigRequest req, [FromServices] OAuthService oauth)
{
var c = await oauth.UpsertConfigAsync(provider, req.ClientId, req.ClientSecret, req.RedirectUri, req.Enabled);
return Ok(new OAuthConfigResponse(provider, c.ClientId, c.RedirectUri, c.Enabled, !string.IsNullOrEmpty(c.ClientSecret)));
}
// ── CRM notes / tags ───────────────────────────────────────────────────────
[HttpGet("v1/users/{userId:guid}/crm")]
public async Task<IActionResult> GetCrm(Guid userId) => Ok(await svc.GetUserCrmAsync(userId));
[HttpPut("v1/users/{userId:guid}/crm")]
public async Task<IActionResult> PutCrm(Guid userId, [FromBody] UpsertUserCrmRequest req)
=> Ok(await svc.UpsertUserCrmAsync(userId, req));
// ── Power-actions ──────────────────────────────────────────────────────────
[HttpPost("v1/users/{userId:guid}/balance")]
public async Task<IActionResult> Balance(Guid userId, [FromBody] SetBalanceRequest req)
{
await svc.SetBalanceAsync(userId, req.AmountMinor, req.Add);
return Ok(new { ok = true });
}
[HttpPost("v1/users/{userId:guid}/password")]
public async Task<IActionResult> Password(Guid userId, [FromBody] ResetPasswordRequest req)
{
await svc.ResetPasswordAsync(userId, req.NewPassword);
return Ok(new { ok = true });
}
[HttpPost("v1/users/{userId:guid}/charge")]
public async Task<IActionResult> Charge(Guid userId, [FromBody] AddChargeRequest req)
{
await svc.AddChargeAsync(userId, req.Seconds, req.RenderCount);
return Ok(new { ok = true });
}
[HttpPost("v1/users/{userId:guid}/moderator")]
public async Task<IActionResult> Moderator(Guid userId, [FromBody] SetFlagRequest req)
{
await svc.SetModeratorAsync(userId, req.Enabled);
return Ok(new { ok = true });
}
[HttpPost("v1/users/{userId:guid}/grant-plan")]
public async Task<IActionResult> GrantPlan(Guid userId, [FromBody] GrantPlanDaysRequest req)
{
try
{
await svc.GrantPlanDaysAsync(userId, req.PlanId, req.Days);
return Ok(new { ok = true });
}
catch (KeyNotFoundException ex)
{
return BadRequest(new { error = new { message = ex.Message } });
}
}
}