Python Script to List Users and Groups

Python script to list users and groups

For *nix, you have the pwd and grp modules. You iterate through pwd.getpwall() to get all users. You look up their group names with grp.getgrgid(gid).

import pwd, grp
for p in pwd.getpwall():
print p[0], grp.getgrgid(p[3])[0]

boto3 - getting a listing of users in each group

First you have to use list_groups to get all groups, and then you can query for details of each group:

response = iam.list_groups()
for group in response['Groups']:
group_details = iam.get_group(GroupName=group['GroupName'])
print(group['GroupName'])
for user in group_details['Users']:
print(" - ", user['UserName'])

How to determine what user and group a Python script is running as?

You can use the following piece of code:

import os
print(os.getegid())

How to list all user groups in Django?

You already got them in user.groups. If you want to show them along with the user, add the groups field to the Meta class fields list, for example:

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
"username",
"first_name",
"last_name",
"email",
"groups",
)

How to know which group a user belongs to

instance.groups.all() will give a QuerySet of groups the user belongs to. So you can check if the user belongs to the administradores with:

@receiver(post_save, sender=User)
def group_user(sender, instance, *args, **kwargs):
if instance.groups:
if instance.groups.filter(name='diurno').exists():
group = Group.objects.get(name='diurno')
instance.groups.clear()
instance.groups.add(group)
else:
group = Group.objects.get(name='nocturno')
instance.groups.clear()
instance.groups.add(group)

By checking with instance.groups == 'diurno' that test is always False, and thus you will each time when you save a user object assign it to the desarrolladores group. By clearing however the groups, a user can never belong to multiple groups.

In your view you can slightly improve the readability with:

class CarListView(LoginRequiredMixin, ListView):
model = Car
login_url = 'users:login'
template_name = 'index.html'
paginate_by = 6

def get_queryset(self, *args, **kwargs):
qs = super().get_queryset(*args, **kwargs)
is_diurno = self.request.user.groups.filter(
name='diurno'
).exists()
if is_diurno:
return qs.annotate(Count('partner')).order_by('-pk')
else:
return qs.filter(user=self.request.user)

I would however strongly advise not to use signals, but add logic to the views where your edit your user. Note that the post_save method will not run when you edit the groups of a user, only when you save the user itself. This thus means that at the moment when you create a user, it will first save the user (without any groups), and after that add the groups to the user. This makes the way signals work less predictable. Here if you thus create a user that belongs to the diurno group: that user will after the signals run belong to both the diurno group and the nocturno group.



Related Topics



Leave a reply



Submit