def get_readonly_fields(self, request, obj=None): if request.resolver_match.url_name == 'app_userinfo_change': if request.user.is_admin(): return [] return settings.READONLY_ATTRIBUTES + UserInfo.other_fields() return super().get_readonly_fields(request, obj)
def get_fieldsets(self, request, obj=None): f = [] if request.resolver_match.url_name == 'app_userinfo_add': f = [ ('用户名', { 'fields': ('uid', ) }), ('密码', { 'fields': (( 'password', 'reset_password', ), ) }), ('属性', { 'fields': ('name', 'mail', 'mobile', 'ssh_key') }), ] elif request.resolver_match.url_name == 'app_userinfo_change': f = [ ('用户名', { 'fields': ('uid', ) }), ('属性', { 'fields': ('name', 'mail', 'mobile', 'ssh_key') }), ] if f and request.user.is_admin(): f.append(('其他', { 'fields': UserInfo.other_fields()[1:], 'classes': ('collapse', ) })) return f or super().get_fieldsets(request, obj)
def get_user_info_dict(user_info, attributes=None, exclude=None): """ :param user_info: :param attributes: None表示未设置attributes,即返回所有attributes :param exclude: :return: """ if exclude is None: exclude = ['id'] + UserInfo.other_fields()[1:] d = user_info_model_to_dict(user_info, exclude) result = {} dn_k = settings.LITE_AUTH_FIELD_MAP.get('uid', 'uid') result['dn'] = dn_k + '=' + user_info.uid if attributes: for k in attributes: result[k] = d.get(k, []) else: result.update(d) if attributes is None or 'groups' in attributes: result['groups'] = list(user_info.groups.values_list('gid', flat=True)) return result
def get_k_v_from_filter_obj(f): if isinstance(f, pureldap.LDAPAttributeValueAssertion): k = f.attributeDesc.value.lower() v = f.assertionValue.value elif isinstance(f, pureldap.LDAPFilter_substrings): k = f.type.lower() v = '' else: raise NotImplementedError('Filter type not supported %r' % f) k = settings.LDAP_FIELD_MAP.get(k, k) # 搜索的条件不存在,则返回 (id,-1) 防止orm报错 if k not in UserInfo.attrs_fields() + UserInfo.other_fields() + ['groups']: return 'id', -1 if k == 'groups': k = 'groups__gid' return k, v
class Meta: model = UserInfo exclude = UserInfo.other_fields()[1:] fields = '__all__'