Exemplo n.º 1
0
    def save(self):
        oldflags = self.instance.flags or 0
        is_creation = self.instance.pk is None
        cig = super().save(commit=False)
        if is_creation:
            cig.contact = self.contact
            cig.group = self.group

        newflags = self.cleaned_data['flags']
        if ((oldflags ^ newflags) & perms.ADMIN_ALL
           and not perms.c_operatorof_cg(self.user.id, self.group.id)):
            # If you change any permission flags of that group, you must be a
            # group operator
            raise PermissionDenied
        if not newflags:
            cig.delete()
            return None
        cig.flags = newflags
        cig.save()
        # TODO: use set_member_1 for logs:
        # cg.set_member_1(request, contact, flags)
        return cig
Exemplo n.º 2
0
    def save(self, commit=True):
        is_creation = self.instance.pk is None
        data = self.cleaned_data

        if is_creation:
            was_sticky = False
        else:
            was_sticky = ContactGroup.objects.get(pk=self.instance.pk).sticky

        # Save the base fields
        cg = super().save(commit)

        # Update the members if it's now sticky
        if cg.sticky and not was_sticky:
            logging.warning("Group %s has become sticky.", cg)
            members = cg.get_all_members()
            members = members.extra(where=["""
                NOT EXISTS (
                    SELECT *
                    FROM contact_in_group
                    WHERE contact_in_group.contact_id=contact.id
                        AND contact_in_group.group_id={group_id}
                        AND flags & {member_flag} <> 0
                )""".format(group_id=cg.id,
                            member_flag=perms.MEMBER)])
            for m in members:
                cg.set_member_1(self.request, m, '+m')

        # Update the super groups
        old_direct_supergroups_ids = set(
            cg.get_visible_direct_supergroups_ids(self.user.id))
        new_direct_supergroups_id = set(
            [int(i) for i in data['direct_supergroups']])
        if cg.id != GROUP_EVERYBODY and not new_direct_supergroups_id:
            new_direct_supergroups_id = {GROUP_EVERYBODY}

        supergroup_added = (new_direct_supergroups_id
                            - old_direct_supergroups_ids)
        supergroup_removed = (old_direct_supergroups_ids
                              - new_direct_supergroups_id)

        print('supergroup_added=', supergroup_added)
        print('supergroup_removed=', supergroup_removed)
        for sgid in supergroup_added:
            GroupInGroup(father_id=sgid, subgroup_id=cg.id).save()
        for sgid in supergroup_removed:
            (GroupInGroup.objects
             .get(father_id=sgid, subgroup_id=cg.id).delete())

        # Update the administrative groups
        for flag in 'oveEcCfFnNuUxX':
            field_name = 'admin_{}_groups'.format(flag)
            intflag = perms.FLAGTOINT[flag]
            old_groups_ids = set(
                cg.get_visible_mananger_groups_ids(self.user.id, intflag))
            new_groups_ids = set([int(ogid) for ogid in data[field_name]])
            # print('flag', flag, 'old_groups_ids', old_groups_ids)
            # print('flag', flag, 'new_groups_ids', new_groups_ids)
            groups_added = new_groups_ids - old_groups_ids
            groups_removed = old_groups_ids - new_groups_ids
            print('flag', flag, 'groups_added=', groups_added)
            print('flag', flag, 'groups_removed=', groups_removed)
            if (not is_creation
               and (groups_added or groups_removed)
               and not perms.c_operatorof_cg(self.user.id, cg.id)):
                # Only operators can change permissions
                raise PermissionDenied
            for ogid in groups_added:
                try:
                    gmg = GroupManageGroup.objects.get(
                        father_id=ogid, subgroup_id=cg.id)
                except GroupManageGroup.DoesNotExist:
                    gmg = GroupManageGroup(
                        father_id=ogid, subgroup_id=cg.id, flags=0)
                gmg.flags |= intflag
                gmg.save()
            for ogid in groups_removed:
                gmg = GroupManageGroup.objects.get(father_id=ogid,
                                                   subgroup_id=cg.id)
                gmg.flags &= ~ intflag
                if gmg.flags:
                    gmg.save()
                else:
                    gmg.delete()

        return cg