Skip to content

Commit

Permalink
init fix of groups page by adaptation to str type rockstor#2564
Browse files Browse the repository at this point in the history
Replace hard chardet dependency with soft
charset-normalizer also used by requests.
Removes redundant encoding on groupname already
encoded.
  • Loading branch information
phillxnet committed Jun 3, 2023
1 parent 9a4dd5f commit b56967c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 24 deletions.
14 changes: 1 addition & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ gunicorn = "==19.10.0" # buildout previously used 19.7.1

# [tool.poetry.group.requests.dependencies]
requests = "==2.27.1" # Last Python 2/3 version, requires chardet
chardet = "==4.0.0" # 5.0.0 requires Python3.
idna = "==2.10" # Requests (2.27.1) requires idna<3,>=2.5
certifi = "==2021.10.8" # Requests (2.27.1) requires certifi>=2017.4.17
urllib3 = "==1.26.12" # Requests (2.27.1) requires urllib3<1.27,>=1.21.1
Expand Down
3 changes: 0 additions & 3 deletions src/rockstor/storageadmin/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import grp

import chardet
from django.conf import settings
from django.contrib.auth.models import User as DjangoUser
from django.core.validators import validate_email
Expand Down Expand Up @@ -58,8 +57,6 @@ def groupname(self, *args, **kwargs):
if self.gid is not None:
try:
groupname = grp.getgrgid(self.gid).gr_name
charset = chardet.detect(groupname)
groupname = groupname.decode(charset["encoding"])
return groupname
except KeyError:
# Failed to fetch user using grp, so let's try with InfoPipe
Expand Down
14 changes: 7 additions & 7 deletions src/rockstor/system/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from shutil import move
from tempfile import mkstemp

import chardet
from charset_normalizer import from_bytes
import dbus
from dbus import DBusException

Expand Down Expand Up @@ -96,8 +96,8 @@ def get_users(max_wait=90):
for u in uf[:-1]:
ufields = u.split(":")
if len(ufields) > 3:
charset = chardet.detect(ufields[0])
uname = ufields[0].decode(charset["encoding"])
charset = from_bytes(ufields[0]).best()
uname = ufields[0].decode(charset)
users[uname] = (int(ufields[2]), int(ufields[3]), str(ufields[6]))
if time.time() - t0 > max_wait:
p.terminate()
Expand All @@ -111,8 +111,8 @@ def get_groups(*gids):
for g in gids:
try:
entry = grp.getgrgid(g)
charset = chardet.detect(entry.gr_name)
gr_name = entry.gr_name.decode(charset["encoding"])
# Assume utf-8 encoded gr_name str
gr_name = entry.gr_name
groups[gr_name] = entry.gr_gid
except KeyError:
# The block above can sometimes fail for domain users (AD/LDAP)
Expand All @@ -130,8 +130,8 @@ def get_groups(*gids):
)
else:
for g in grp.getgrall():
charset = chardet.detect(g.gr_name)
gr_name = g.gr_name.decode(charset["encoding"])
# Assume utf-8 encoded gr_name str
gr_name = g.gr_name
groups[gr_name] = g.gr_gid

# If sssd.service is running:
Expand Down

0 comments on commit b56967c

Please sign in to comment.