Change the groups this user belongs to, add append=False to make the user a member of only the specified groups Args: name (str): The username for which to change groups groups (str, list): A single group or a list of groups to assign to the user. For
(name, groups, append=True)
| 704 | |
| 705 | |
| 706 | def chgroups(name, groups, append=True): |
| 707 | """ |
| 708 | Change the groups this user belongs to, add append=False to make the user a |
| 709 | member of only the specified groups |
| 710 | |
| 711 | Args: |
| 712 | |
| 713 | name (str): The username for which to change groups |
| 714 | |
| 715 | groups (str, list): |
| 716 | A single group or a list of groups to assign to the user. For |
| 717 | multiple groups this can be a comma-delimited string or a list. |
| 718 | |
| 719 | append (:obj:`bool`, optional): |
| 720 | ``True`` adds the passed groups to the user's current groups. |
| 721 | ``False`` sets the user's groups to the passed groups only. |
| 722 | |
| 723 | Default is ``True``. |
| 724 | |
| 725 | Returns: |
| 726 | bool: ``True`` if successful, otherwise ``False``. |
| 727 | |
| 728 | CLI Example: |
| 729 | |
| 730 | .. code-block:: bash |
| 731 | |
| 732 | salt '*' user.chgroups jsnuffy Administrators,Users True |
| 733 | """ |
| 734 | if isinstance(groups, str): |
| 735 | groups = groups.split(",") |
| 736 | |
| 737 | groups = [x.strip(" *") for x in groups] |
| 738 | current_groups = set(list_groups(name)) |
| 739 | expected_groups = set() |
| 740 | |
| 741 | name = shlex.quote(str(name)) |
| 742 | |
| 743 | if not append: |
| 744 | # We don't want to append to the list, remove groups not in the new set |
| 745 | # of groups |
| 746 | for group in current_groups: |
| 747 | group = shlex.quote(group).lstrip("'").rstrip("'") |
| 748 | if group not in groups: |
| 749 | cmd = f'net localgroup "{group}" {name} /delete' |
| 750 | __salt__["cmd.run_all"](cmd, python_shell=True) |
| 751 | else: |
| 752 | expected_groups.add(group) |
| 753 | else: |
| 754 | # We're appending to the current list of groups. If they already match |
| 755 | # then bail |
| 756 | if current_groups == set(groups): |
| 757 | return True |
| 758 | else: |
| 759 | expected_groups = current_groups.union(set(groups)) |
| 760 | |
| 761 | for group in groups: |
| 762 | if group in current_groups: |
| 763 | continue |
no test coverage detected