繁体   English   中英

通过Python和Django中的GUI修改LDAP用户

[英]Modify LDAP user via a GUI in Python and Django

我正在使用一个Web应用程序供LDAP用户登录并编辑其属性,例如用户名,描述等。我想知道是否有一种方法可以修改登录用户的属性。 python-ldap3库的Modify运算符是解决方法,但是我只能在代码本身中修改指定的用户。

是否有可能使此功能成为已登录用户的常规功能? 我在寻找解决方案,但找不到任何东西。

这是我的修改运算符(modify.py):

from ldap3 import Server, Connection, ALL, MODIFY_REPLACE

s = Server('192.168.1.154', get_info=ALL)

c = Connection(s, 'cn=ldap_user,cn=Users,dc=domain,dc=com', 'Password', auto_bind=False)
c.bind()

c.modify('cn=ldap_user,cn=Users,dc=domain,dc=com',
        {'givenName': [(MODIFY_REPLACE, ['Test'])],
         'sn': [(MODIFY_REPLACE, ['User'])]})
print(c.result)

c.unbind()

顺便说一句,我使用的是Django,所以在这一点上,我什至不知道该做什么和做什么。

我的edit_profile视图:

def edit_profile(request):
if request.method == 'POST':
    form = EditProfileForm(request.POST, instance=request.user)

    if form.is_valid():
        form.save()
        return redirect(reverse('accounts:view_profile'))
else:
    form = EditProfileForm(instance=request.user)
args = {'form': form}
return render(request, 'accounts/edit_profile.html', args)

我的EditProfileForm:

class EditProfileForm(UserChangeForm):
first_name = forms.CharField(max_length=30, required=False,
widget=forms.TextInput(
        attrs={
            'class': 'form-control',
            'type': 'text',
            'name': 'first_name',

        }
    )
)

last_name = forms.CharField(max_length=30, required=False,
widget=forms.TextInput(
        attrs={
            'class': 'form-control',
            'type': 'text',
            'name': 'last_name',
        }
    )
)

email = forms.EmailField(max_length=30, required=True,
widget=forms.TextInput(
        attrs={
            'class': 'form-control',
            'type': 'email',
            'name': 'email',
        }
    )
)

password = forms.CharField(required=False,
widget=forms.PasswordInput(
        attrs={
            'class': 'form-control',
            'type': 'hidden',
            'name': 'password',
        }
    )
)

class Meta:
    model = LdapUser
    fields = (
        'first_name',
        'last_name',
        'email',
        'password'
    )

可以使用Connection和声明用于修改属性的方法来组成新的ModifiableConnection类。 例如

from ldap3 import Server, Connection, ALL, MODIFY_REPLACE


class ModifiableConnection(object):
    def __init__(self, *args, **kwargs):
        self.conn = Connection(*args, **kwargs)

    def _modify_attr(self, attrname, values):
         attr = {attrname: [(MODIFY_REPLACE, values)]}
         self.conn.modify(self.conn.user, attr)

    def unbind(self):
         self.conn.unbind()

    @property
    def firstname(self): pass

    @firstname.setter
    def firstname(self, val):
        self.modify_attr('givenName', [val])

    @property
    def lastname(self): pass

    @lastname.setter
    def lastname(self, val):
        self.modify_attr('sn', [val])



    def bind(self):
         self.conn.bind()

server = Server('192.168.1.154', get_info=ALL)

user = 'cn=ldap_user,cn=Users,dc=domain,dc=com'
mod_con = ModifiableConnection(server, user, 'Password', auto_bind=False)
mod_con.bind()

mod_con.firstname = 'Test'
mod_con.lastname = 'User'

mod_con.unbind()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM