Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STY: Use the UserAccessPermissions enum #2398

Merged
merged 3 commits into from
Jan 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
from .constants import (
CheckboxRadioButtonAttributes,
GoToActionArguments,
UserAccessPermissions,
)
from .constants import Core as CO
from .constants import DocumentInformationAttributes as DI
Expand Down Expand Up @@ -1809,19 +1810,22 @@ def decrypt(self, password: Union[str, bytes]) -> PasswordType:
return self._encryption.verify(password)

def decode_permissions(self, permissions_code: int) -> Dict[str, bool]:
# Takes the permissions as an integer, returns the allowed access
permissions = {}
permissions["print"] = permissions_code & (1 << 3 - 1) != 0 # bit 3
permissions["modify"] = permissions_code & (1 << 4 - 1) != 0 # bit 4
permissions["copy"] = permissions_code & (1 << 5 - 1) != 0 # bit 5
permissions["annotations"] = permissions_code & (1 << 6 - 1) != 0 # bit 6
permissions["forms"] = permissions_code & (1 << 9 - 1) != 0 # bit 9
permissions["accessability"] = permissions_code & (1 << 10 - 1) != 0 # bit 10
permissions["assemble"] = permissions_code & (1 << 11 - 1) != 0 # bit 11
permissions["print_high_quality"] = (
permissions_code & (1 << 12 - 1) != 0
) # bit 12
return permissions
"""Take the permissions as an integer, return the allowed access."""
permissions_mapping = {
"print": UserAccessPermissions.PRINT,
"modify": UserAccessPermissions.MODIFY,
"copy": UserAccessPermissions.EXTRACT,
"annotations": UserAccessPermissions.ADD_OR_MODIFY,
"forms": UserAccessPermissions.R7,
"accessability": UserAccessPermissions.EXTRACT_TEXT_AND_GRAPHICS,
Comment on lines +1817 to +1820
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't even know if the string versions are correct.

"assemble": UserAccessPermissions.ASSEMBLE_DOC,
"print_high_quality": UserAccessPermissions.PRINT_TO_REPRESENTATION,
}

return {
key: permissions_code & flag != 0
for key, flag in permissions_mapping.items()
}

@property
def is_encrypted(self) -> bool:
Expand Down
Loading