Ejemplo n.º 1
0
    def create(self, request, *args, **kwargs):  # pylint: disable=unused-argument
        '''
        create user [POST]
        '''
        data = request.data
        user_info = data.get('user', '')

        group_uids = []
        dept_uids = []

        node_uids = data.get('node_uids', [])
        if node_uids:
            for node_uid in node_uids:
                if node_uid.startswith(Dept.NODE_PREFIX):
                    dept_uids.append(node_uid.replace(Dept.NODE_PREFIX, '', 1))
                elif node_uid.startswith(Group.NODE_PREFIX):
                    group_uids.append(node_uid.replace(
                        Group.NODE_PREFIX,
                        '',
                    ))
        else:
            group_uids = data.get('group_uids', [])
            dept_uids = data.get('dept_uids', [])

        user_info.pop('password', None)
        user = CLI().create_user(user_info)
        user.origin = 1  # 管理员添加
        user.save()

        self.assign_user(user, dept_uids=dept_uids, group_uids=group_uids)

        user_serializer = EmployeeSerializer(user)
        return Response(user_serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=self.get_success_headers(user_serializer.data))
Ejemplo n.º 2
0
def generate_user_payload(user, from_register=False):
    if from_register:
        data = UserWithPermSerializer(user).data
        roles = data.get('roles', [])
        data['roles'] = list(roles)
    else:
        data = EmployeeSerializer(user).data
    return json.dumps(data)
Ejemplo n.º 3
0
 def update(self, request, *args, **kwargs):    # pylint: disable=unused-argument
     '''
     update user detail [PATCH]
     '''
     user = self.get_object()
     data = request.data
     user = CLI().update_user(user, data)
     user_serializer = EmployeeSerializer(user)
     return Response(user_serializer.data)
Ejemplo n.º 4
0
    def create(self, request, *args, **kwargs):  # pylint: disable=unused-argument
        '''
        create user [POST]
        '''
        data = request.data
        user_info = data.get('user', '')

        group_uids = []
        dept_uids = []

        node_uids = data.get('node_uids', [])
        if node_uids:
            for node_uid in node_uids:
                if node_uid.startswith(Dept.NODE_PREFIX):
                    dept_uids.append(node_uid.replace(Dept.NODE_PREFIX, '', 1))
                elif node_uid.startswith(Group.NODE_PREFIX):
                    group_uids.append(node_uid.replace(
                        Group.NODE_PREFIX,
                        '',
                    ))
        else:
            group_uids = data.get('group_uids', [])
            dept_uids = data.get('dept_uids', [])
        cli = CLI()
        password = user_info.pop('password', None)
        # 创建用户
        user = cli.create_user(user_info)
        if password:
            validate_password(password)
            cli.set_user_password(user, password)
        user.origin = 1  # 管理员添加
        user.save()
        # 分配组和部门
        self.assign_user(user, dept_uids=dept_uids, group_uids=group_uids)
        user_serializer = EmployeeSerializer(user)
        transaction.on_commit(lambda: WebhookManager.user_created(user))
        return Response(
            user_serializer.data,
            status=status.HTTP_201_CREATED,
            headers=self.get_success_headers(user_serializer.data),
        )
Ejemplo n.º 5
0
 def retrieve(self, request, *args, **kwargs):
     '''
     return user detail [GET]
     '''
     user = self.get_object()
     return Response(EmployeeSerializer(user).data)