Account type
This commit is contained in:
parent
c798083eb5
commit
f07170a82f
@ -37,7 +37,16 @@
|
|||||||
@for (account of mailAccounts; track account) {
|
@for (account of mailAccounts; track account) {
|
||||||
<li class="list-group-item">
|
<li class="list-group-item">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col d-flex align-items-center"><button class="btn btn-link p-0 text-start" type="button" (click)="editMailAccount(account)" [disabled]="!permissionsService.currentUserCan(PermissionAction.Change, PermissionType.MailAccount)">{{account.name}}</button></div>
|
<div class="col d-flex align-items-center">
|
||||||
|
<button class="btn btn-link p-0 text-start" type="button" (click)="editMailAccount(account)" [disabled]="!permissionsService.currentUserCan(PermissionAction.Change, PermissionType.MailAccount)">
|
||||||
|
{{account.name}}
|
||||||
|
@if (account.account_type === MailAccountType.Gmail) {
|
||||||
|
<i-bs name="google" class="ms-2"></i-bs>
|
||||||
|
} @else if (account.account_type === MailAccountType.Outlook) {
|
||||||
|
<i-bs name="microsoft" class="ms-2"></i-bs>
|
||||||
|
}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
<div class="col d-flex align-items-center">{{account.imap_server}}</div>
|
<div class="col d-flex align-items-center">{{account.imap_server}}</div>
|
||||||
<div class="col d-flex align-items-center d-none d-sm-block">{{account.username}}</div>
|
<div class="col d-flex align-items-center d-none d-sm-block">{{account.username}}</div>
|
||||||
<div class="col">
|
<div class="col">
|
||||||
|
@ -2,7 +2,7 @@ import { Component, OnInit, OnDestroy } from '@angular/core'
|
|||||||
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
||||||
import { Subject, first, takeUntil } from 'rxjs'
|
import { Subject, first, takeUntil } from 'rxjs'
|
||||||
import { ObjectWithPermissions } from 'src/app/data/object-with-permissions'
|
import { ObjectWithPermissions } from 'src/app/data/object-with-permissions'
|
||||||
import { MailAccount } from 'src/app/data/mail-account'
|
import { MailAccount, MailAccountType } from 'src/app/data/mail-account'
|
||||||
import { MailRule } from 'src/app/data/mail-rule'
|
import { MailRule } from 'src/app/data/mail-rule'
|
||||||
import {
|
import {
|
||||||
PermissionsService,
|
PermissionsService,
|
||||||
@ -31,6 +31,8 @@ export class MailComponent
|
|||||||
extends ComponentWithPermissions
|
extends ComponentWithPermissions
|
||||||
implements OnInit, OnDestroy
|
implements OnInit, OnDestroy
|
||||||
{
|
{
|
||||||
|
public MailAccountType = MailAccountType
|
||||||
|
|
||||||
mailAccounts: MailAccount[] = []
|
mailAccounts: MailAccount[] = []
|
||||||
mailRules: MailRule[] = []
|
mailRules: MailRule[] = []
|
||||||
|
|
||||||
|
@ -6,6 +6,12 @@ export enum IMAPSecurity {
|
|||||||
STARTTLS = 3,
|
STARTTLS = 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum MailAccountType {
|
||||||
|
IMAP = 1,
|
||||||
|
Gmail = 2,
|
||||||
|
Outlook = 3,
|
||||||
|
}
|
||||||
|
|
||||||
export interface MailAccount extends ObjectWithPermissions {
|
export interface MailAccount extends ObjectWithPermissions {
|
||||||
name: string
|
name: string
|
||||||
|
|
||||||
@ -22,4 +28,10 @@ export interface MailAccount extends ObjectWithPermissions {
|
|||||||
character_set?: string
|
character_set?: string
|
||||||
|
|
||||||
is_token: boolean
|
is_token: boolean
|
||||||
|
|
||||||
|
account_type: MailAccountType
|
||||||
|
|
||||||
|
refresh_token?: string
|
||||||
|
|
||||||
|
expiration?: string // Date
|
||||||
}
|
}
|
||||||
|
@ -247,11 +247,11 @@ export const SETTINGS: UiSetting[] = [
|
|||||||
{
|
{
|
||||||
key: SETTINGS_KEYS.GMAIL_OAUTH_URL,
|
key: SETTINGS_KEYS.GMAIL_OAUTH_URL,
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: null,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: SETTINGS_KEYS.OUTLOOK_OAUTH_URL,
|
key: SETTINGS_KEYS.OUTLOOK_OAUTH_URL,
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: null,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
@ -8,7 +8,7 @@ class TestMigrateWorkflow(TestMigrations):
|
|||||||
dependencies = (
|
dependencies = (
|
||||||
(
|
(
|
||||||
"paperless_mail",
|
"paperless_mail",
|
||||||
"0027_mailaccount_expiration_mailaccount_refresh_token",
|
"0027_mailaccount_expiration_mailaccount_account_type_and_more",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -431,7 +431,7 @@ def refresh_oauth_token(self, account: MailAccount) -> bool:
|
|||||||
self.log.error(f"Account {account}: No refresh token available.")
|
self.log.error(f"Account {account}: No refresh token available.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "gmail" in account.imap_server:
|
if account.account_type == MailAccount.MailAccountType.GMAIL:
|
||||||
url = "https://accounts.google.com/o/oauth2/token"
|
url = "https://accounts.google.com/o/oauth2/token"
|
||||||
data = {
|
data = {
|
||||||
"client_id": settings.GMAIL_OAUTH_CLIENT_ID,
|
"client_id": settings.GMAIL_OAUTH_CLIENT_ID,
|
||||||
@ -439,7 +439,7 @@ def refresh_oauth_token(self, account: MailAccount) -> bool:
|
|||||||
"refresh_token": account.refresh_token,
|
"refresh_token": account.refresh_token,
|
||||||
"grant_type": "refresh_token",
|
"grant_type": "refresh_token",
|
||||||
}
|
}
|
||||||
elif "outlook" in account.imap_server:
|
elif account.account_type == MailAccount.MailAccountType.OUTLOOK:
|
||||||
url = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
|
url = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
|
||||||
data = {
|
data = {
|
||||||
"client_id": settings.OUTLOOK_OAUTH_CLIENT_ID,
|
"client_id": settings.OUTLOOK_OAUTH_CLIENT_ID,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# Generated by Django 5.1.1 on 2024-10-05 07:42
|
# Generated by Django 5.1.1 on 2024-10-05 17:12
|
||||||
|
|
||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@ -20,6 +20,15 @@ class Migration(migrations.Migration):
|
|||||||
verbose_name="expiration",
|
verbose_name="expiration",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="mailaccount",
|
||||||
|
name="account_type",
|
||||||
|
field=models.PositiveIntegerField(
|
||||||
|
choices=[(1, "IMAP"), (2, "Gmail"), (3, "Outlook")],
|
||||||
|
default=1,
|
||||||
|
verbose_name="account type",
|
||||||
|
),
|
||||||
|
),
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name="mailaccount",
|
model_name="mailaccount",
|
||||||
name="refresh_token",
|
name="refresh_token",
|
@ -15,6 +15,11 @@ class MailAccount(document_models.ModelWithOwner):
|
|||||||
SSL = 2, _("Use SSL")
|
SSL = 2, _("Use SSL")
|
||||||
STARTTLS = 3, _("Use STARTTLS")
|
STARTTLS = 3, _("Use STARTTLS")
|
||||||
|
|
||||||
|
class MailAccountType(models.IntegerChoices):
|
||||||
|
IMAP = 1, _("IMAP")
|
||||||
|
GMAIL = 2, _("Gmail")
|
||||||
|
OUTLOOK = 3, _("Outlook")
|
||||||
|
|
||||||
name = models.CharField(_("name"), max_length=256, unique=True)
|
name = models.CharField(_("name"), max_length=256, unique=True)
|
||||||
|
|
||||||
imap_server = models.CharField(_("IMAP server"), max_length=256)
|
imap_server = models.CharField(_("IMAP server"), max_length=256)
|
||||||
@ -51,6 +56,12 @@ class MailAccount(document_models.ModelWithOwner):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
account_type = models.PositiveIntegerField(
|
||||||
|
_("account type"),
|
||||||
|
choices=MailAccountType.choices,
|
||||||
|
default=MailAccountType.IMAP,
|
||||||
|
)
|
||||||
|
|
||||||
refresh_token = models.CharField(
|
refresh_token = models.CharField(
|
||||||
_("refresh token"),
|
_("refresh token"),
|
||||||
max_length=2048,
|
max_length=2048,
|
||||||
|
@ -39,6 +39,9 @@ class MailAccountSerializer(OwnedObjectSerializer):
|
|||||||
"user_can_change",
|
"user_can_change",
|
||||||
"permissions",
|
"permissions",
|
||||||
"set_permissions",
|
"set_permissions",
|
||||||
|
"account_type",
|
||||||
|
"refresh_token",
|
||||||
|
"expiration",
|
||||||
]
|
]
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
def update(self, instance, validated_data):
|
||||||
|
@ -63,6 +63,7 @@ class MailAccountTestView(GenericAPIView):
|
|||||||
):
|
):
|
||||||
existing_account = MailAccount.objects.get(pk=request.data["id"])
|
existing_account = MailAccount.objects.get(pk=request.data["id"])
|
||||||
serializer.validated_data["password"] = existing_account.password
|
serializer.validated_data["password"] = existing_account.password
|
||||||
|
serializer.validated_data["account_type"] = existing_account.account_type
|
||||||
serializer.validated_data["refresh_token"] = existing_account.refresh_token
|
serializer.validated_data["refresh_token"] = existing_account.refresh_token
|
||||||
serializer.validated_data["expiration"] = existing_account.expiration
|
serializer.validated_data["expiration"] = existing_account.expiration
|
||||||
|
|
||||||
@ -109,12 +110,14 @@ class OauthCallbackView(GenericAPIView):
|
|||||||
if scope is not None and "google" in scope:
|
if scope is not None and "google" in scope:
|
||||||
# Google
|
# Google
|
||||||
# Gmail setup guide: https://postmansmtp.com/how-to-configure-post-smtp-with-gmailgsuite-using-oauth/
|
# Gmail setup guide: https://postmansmtp.com/how-to-configure-post-smtp-with-gmailgsuite-using-oauth/
|
||||||
|
account_type = MailAccount.AccountType.GMAIL
|
||||||
imap_server = "imap.gmail.com"
|
imap_server = "imap.gmail.com"
|
||||||
defaults = {
|
defaults = {
|
||||||
"name": f"Gmail OAuth {datetime.now()}",
|
"name": f"Gmail OAuth {datetime.now()}",
|
||||||
"username": "",
|
"username": "",
|
||||||
"imap_security": MailAccount.ImapSecurity.SSL,
|
"imap_security": MailAccount.ImapSecurity.SSL,
|
||||||
"imap_port": 993,
|
"imap_port": 993,
|
||||||
|
"account_type": account_type,
|
||||||
}
|
}
|
||||||
|
|
||||||
token_request_uri = "https://accounts.google.com/o/oauth2/token"
|
token_request_uri = "https://accounts.google.com/o/oauth2/token"
|
||||||
@ -124,12 +127,14 @@ class OauthCallbackView(GenericAPIView):
|
|||||||
elif scope is None:
|
elif scope is None:
|
||||||
# Outlook
|
# Outlook
|
||||||
# Outlok setup guide: https://medium.com/@manojkumardhakad/python-read-and-send-outlook-mail-using-oauth2-token-and-graph-api-53de606ecfa1
|
# Outlok setup guide: https://medium.com/@manojkumardhakad/python-read-and-send-outlook-mail-using-oauth2-token-and-graph-api-53de606ecfa1
|
||||||
|
account_type = MailAccount.AccountType.OUTLOOK
|
||||||
imap_server = "outlook.office365.com"
|
imap_server = "outlook.office365.com"
|
||||||
defaults = {
|
defaults = {
|
||||||
"name": f"Outlook OAuth {datetime.now()}",
|
"name": f"Outlook OAuth {datetime.now()}",
|
||||||
"username": "",
|
"username": "",
|
||||||
"imap_security": MailAccount.ImapSecurity.SSL,
|
"imap_security": MailAccount.ImapSecurity.SSL,
|
||||||
"imap_port": 993,
|
"imap_port": 993,
|
||||||
|
"account_type": account_type,
|
||||||
}
|
}
|
||||||
|
|
||||||
token_request_uri = (
|
token_request_uri = (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user