Merge pull request #3347 from paperless-ngx/fix/issue-3346

Fix: default frontend to current owner, allow setting no owner on create
This commit is contained in:
shamoon 2023-05-10 08:18:08 -07:00 committed by GitHub
commit f78f212a77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 116 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import { DEFAULT_MATCHING_ALGORITHM } from 'src/app/data/matching-model'
import { PaperlessCorrespondent } from 'src/app/data/paperless-correspondent'
import { CorrespondentService } from 'src/app/services/rest/correspondent.service'
import { UserService } from 'src/app/services/rest/user.service'
import { SettingsService } from 'src/app/services/settings.service'
@Component({
selector: 'app-correspondent-edit-dialog',
@ -16,9 +17,10 @@ export class CorrespondentEditDialogComponent extends EditDialogComponent<Paperl
constructor(
service: CorrespondentService,
activeModal: NgbActiveModal,
userService: UserService
userService: UserService,
settingsService: SettingsService
) {
super(service, activeModal, userService)
super(service, activeModal, userService, settingsService)
}
getCreateTitle() {

View File

@ -6,6 +6,7 @@ import { DEFAULT_MATCHING_ALGORITHM } from 'src/app/data/matching-model'
import { PaperlessDocumentType } from 'src/app/data/paperless-document-type'
import { DocumentTypeService } from 'src/app/services/rest/document-type.service'
import { UserService } from 'src/app/services/rest/user.service'
import { SettingsService } from 'src/app/services/settings.service'
@Component({
selector: 'app-document-type-edit-dialog',
@ -16,9 +17,10 @@ export class DocumentTypeEditDialogComponent extends EditDialogComponent<Paperle
constructor(
service: DocumentTypeService,
activeModal: NgbActiveModal,
userService: UserService
userService: UserService,
settingsService: SettingsService
) {
super(service, activeModal, userService)
super(service, activeModal, userService, settingsService)
}
getCreateTitle() {

View File

@ -13,6 +13,7 @@ import { PaperlessUser } from 'src/app/data/paperless-user'
import { AbstractPaperlessService } from 'src/app/services/rest/abstract-paperless-service'
import { UserService } from 'src/app/services/rest/user.service'
import { PermissionsFormObject } from '../input/permissions/permissions-form/permissions-form.component'
import { SettingsService } from 'src/app/services/settings.service'
@Directive()
export abstract class EditDialogComponent<
@ -22,7 +23,8 @@ export abstract class EditDialogComponent<
constructor(
protected service: AbstractPaperlessService<T>,
private activeModal: NgbActiveModal,
private userService: UserService
private userService: UserService,
private settingsService: SettingsService
) {}
users: PaperlessUser[]
@ -64,7 +66,14 @@ export abstract class EditDialogComponent<
this.closeEnabled = true
})
this.userService.listAll().subscribe((r) => (this.users = r.results))
this.userService.listAll().subscribe((r) => {
this.users = r.results
if (this.dialogMode === 'create') {
this.objectForm.get('permissions_form').setValue({
owner: this.settingsService.currentUser.id,
})
}
})
}
getCreateTitle() {

View File

@ -5,6 +5,7 @@ import { EditDialogComponent } from 'src/app/components/common/edit-dialog/edit-
import { PaperlessGroup } from 'src/app/data/paperless-group'
import { GroupService } from 'src/app/services/rest/group.service'
import { UserService } from 'src/app/services/rest/user.service'
import { SettingsService } from 'src/app/services/settings.service'
@Component({
selector: 'app-group-edit-dialog',
@ -15,9 +16,10 @@ export class GroupEditDialogComponent extends EditDialogComponent<PaperlessGroup
constructor(
service: GroupService,
activeModal: NgbActiveModal,
userService: UserService
userService: UserService,
settingsService: SettingsService
) {
super(service, activeModal, userService)
super(service, activeModal, userService, settingsService)
}
getCreateTitle() {

View File

@ -8,6 +8,7 @@ import {
} from 'src/app/data/paperless-mail-account'
import { MailAccountService } from 'src/app/services/rest/mail-account.service'
import { UserService } from 'src/app/services/rest/user.service'
import { SettingsService } from 'src/app/services/settings.service'
const IMAP_SECURITY_OPTIONS = [
{ id: IMAPSecurity.None, name: $localize`No encryption` },
@ -30,9 +31,10 @@ export class MailAccountEditDialogComponent extends EditDialogComponent<Paperles
constructor(
service: MailAccountService,
activeModal: NgbActiveModal,
userService: UserService
userService: UserService,
settingsService: SettingsService
) {
super(service, activeModal, userService)
super(service, activeModal, userService, settingsService)
}
getCreateTitle() {

View File

@ -19,6 +19,7 @@ import { DocumentTypeService } from 'src/app/services/rest/document-type.service
import { MailAccountService } from 'src/app/services/rest/mail-account.service'
import { MailRuleService } from 'src/app/services/rest/mail-rule.service'
import { UserService } from 'src/app/services/rest/user.service'
import { SettingsService } from 'src/app/services/settings.service'
const ATTACHMENT_TYPE_OPTIONS = [
{
@ -115,9 +116,10 @@ export class MailRuleEditDialogComponent extends EditDialogComponent<PaperlessMa
accountService: MailAccountService,
correspondentService: CorrespondentService,
documentTypeService: DocumentTypeService,
userService: UserService
userService: UserService,
settingsService: SettingsService
) {
super(service, activeModal, userService)
super(service, activeModal, userService, settingsService)
accountService
.listAll()

View File

@ -6,6 +6,7 @@ import { DEFAULT_MATCHING_ALGORITHM } from 'src/app/data/matching-model'
import { PaperlessStoragePath } from 'src/app/data/paperless-storage-path'
import { StoragePathService } from 'src/app/services/rest/storage-path.service'
import { UserService } from 'src/app/services/rest/user.service'
import { SettingsService } from 'src/app/services/settings.service'
@Component({
selector: 'app-storage-path-edit-dialog',
@ -16,9 +17,10 @@ export class StoragePathEditDialogComponent extends EditDialogComponent<Paperles
constructor(
service: StoragePathService,
activeModal: NgbActiveModal,
userService: UserService
userService: UserService,
settingsService: SettingsService
) {
super(service, activeModal, userService)
super(service, activeModal, userService, settingsService)
}
get pathHint() {

View File

@ -7,6 +7,7 @@ import { TagService } from 'src/app/services/rest/tag.service'
import { randomColor } from 'src/app/utils/color'
import { DEFAULT_MATCHING_ALGORITHM } from 'src/app/data/matching-model'
import { UserService } from 'src/app/services/rest/user.service'
import { SettingsService } from 'src/app/services/settings.service'
@Component({
selector: 'app-tag-edit-dialog',
@ -17,9 +18,10 @@ export class TagEditDialogComponent extends EditDialogComponent<PaperlessTag> {
constructor(
service: TagService,
activeModal: NgbActiveModal,
userService: UserService
userService: UserService,
settingsService: SettingsService
) {
super(service, activeModal, userService)
super(service, activeModal, userService, settingsService)
}
getCreateTitle() {

View File

@ -7,6 +7,7 @@ import { PaperlessGroup } from 'src/app/data/paperless-group'
import { PaperlessUser } from 'src/app/data/paperless-user'
import { GroupService } from 'src/app/services/rest/group.service'
import { UserService } from 'src/app/services/rest/user.service'
import { SettingsService } from 'src/app/services/settings.service'
@Component({
selector: 'app-user-edit-dialog',
@ -23,9 +24,10 @@ export class UserEditDialogComponent
constructor(
service: UserService,
activeModal: NgbActiveModal,
groupsService: GroupService
groupsService: GroupService,
settingsService: SettingsService
) {
super(service, activeModal, service)
super(service, activeModal, service, settingsService)
groupsService
.listAll()

View File

@ -220,6 +220,8 @@ class OwnedObjectSerializer(serializers.ModelSerializer, SetPermissionsMixin):
permissions = None
if "set_permissions" in validated_data:
permissions = validated_data.pop("set_permissions")
if "user" not in permissions or permissions["user"] is None:
validated_data["owner"] = None
instance = super().create(validated_data)
if permissions is not None:
self._set_permissions(permissions, instance)

View File

@ -3550,6 +3550,77 @@ class TestApiAuth(DirectoriesMixin, APITestCase):
status.HTTP_404_NOT_FOUND,
)
def test_api_set_permissions(self):
"""
GIVEN:
- API request to create an object (Tag) that supplies set_permissions object
WHEN:
- owner is passed as null or as a user id
- view > users is set
THEN:
- Object permissions are set appropriately
"""
user1 = User.objects.create_superuser(username="user1")
user2 = User.objects.create(username="user2")
self.client.force_authenticate(user1)
response = self.client.post(
"/api/tags/",
json.dumps(
{
"name": "test1",
"matching_algorithm": MatchingModel.MATCH_AUTO,
"set_permissions": {
"owner": None,
"view": {
"users": None,
"groups": None,
},
"change": {
"users": None,
"groups": None,
},
},
},
),
content_type="application/json",
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
tag1 = Tag.objects.filter(name="test1").first()
self.assertEqual(tag1.owner, None)
response = self.client.post(
"/api/tags/",
json.dumps(
{
"name": "test2",
"matching_algorithm": MatchingModel.MATCH_AUTO,
"set_permissions": {
"owner": user1.id,
"view": {
"users": [user2.id],
"groups": None,
},
"change": {
"users": None,
"groups": None,
},
},
},
),
content_type="application/json",
)
tag2 = Tag.objects.filter(name="test2").first()
from guardian.core import ObjectPermissionChecker
checker = ObjectPermissionChecker(user2)
self.assertEqual(checker.has_perm("view_tag", tag2), True)
def test_dynamic_permissions_fields(self):
user1 = User.objects.create_user(username="user1")
user1.user_permissions.add(*Permission.objects.filter(codename="view_document"))