# Admin Feature Documentation

## Overview

The admin feature allows specific users to manage all bots in the system, regardless of ownership. Admin users can start, stop, delete, and modify any bot without owning the associated account.

## How It Works

Admin privileges are granted through environment variables, making it secure and environment-specific. The system checks if a user is an admin by comparing their username against a comma-separated list of admin usernames configured in the environment.

## Configuration

### Environment Variable
Set the `AdminUsers` environment variable with a comma-separated list of usernames:

```bash
AdminUsers=admin1,superuser,john.doe
```

### CapRover Configuration
In your CapRover dashboard:
1. Go to your app's settings
2. Navigate to "Environment Variables"
3. Add a new environment variable:
   - Key: `AdminUsers`
   - Value: `admin1,superuser,john.doe` (replace with actual admin usernames)

### Local Development
For local development, you can set this in your `appsettings.Development.json`:

```json
{
  "AdminUsers": "admin1,superuser,john.doe"
}
```

## Admin Capabilities

Admin users can perform all bot operations without ownership restrictions:

- **Start/Save Bot**: Create and start bots for any account
- **Stop Bot**: Stop any running bot
- **Delete Bot**: Delete any bot
- **Restart Bot**: Restart any bot
- **Open/Close Positions**: Manually open or close positions for any bot
- **Update Configuration**: Modify any bot's configuration
- **View Bot Configuration**: Access any bot's configuration details

## Security Notes

1. **Environment-Based**: Admin users are configured via environment variables, not through the API
2. **No Privilege Escalation**: Regular users cannot grant themselves admin access
3. **Audit Logging**: All admin actions are logged with the admin user's context
4. **Case-Insensitive**: Username matching is case-insensitive for convenience

## Implementation Details

The admin feature is implemented using:
- `IAdminConfigurationService`: Checks if a user is an admin
- Updated `UserOwnsBotAccount` method: Returns true for admin users
- Dependency injection: Service is registered as a singleton
- Configuration reading: Reads from `AdminUsers` environment variable

## Example Usage

1. **Set Admin Users**:
   ```bash
   AdminUsers=alice,bob,charlie
   ```

2. **Admin Operations**:
   - Alice, Bob, or Charlie can now manage any bot in the system
   - They can use all existing bot endpoints without ownership restrictions
   - All operations are logged with their username for audit purposes

## Troubleshooting

- **Admin not working**: Check if the username exactly matches the configuration (case-insensitive)
- **No admins configured**: Check the `AdminUsers` environment variable is set
- **Multiple environments**: Each environment (dev, staging, prod) should have its own admin configuration
