[英]Modify LDAP user via a GUI in Python and Django
I am working on a web application for LDAP users to login and edit their attributes such as username, description etc. And I was wondering if there is a way to modify the attributes of the logged in user. 我正在使用一个Web应用程序供LDAP用户登录并编辑其属性,例如用户名,描述等。我想知道是否有一种方法可以修改登录用户的属性。 The modify operator of the python-ldap3 library is the way to go, but I can only modify a specified user in the code itself.
python-ldap3库的Modify运算符是解决方法,但是我只能在代码本身中修改指定的用户。
Is it even possible to make this a general function for a logged in user or not? 是否有可能使此功能成为已登录用户的常规功能? I searched for solutions, but I could not find anything.
我在寻找解决方案,但找不到任何东西。
This is my modify operator (modify.py): 这是我的修改运算符(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()
By the way, I am using Django, so at this point, I do not even know where and what I have to do. 顺便说一句,我使用的是Django,所以在这一点上,我什至不知道该做什么和做什么。
My edit_profile view: 我的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)
my EditProfileForm: 我的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'
)
A new ModifiableConnection
class can be composed using Connection
and a method declared to modify attributes. 可以使用
Connection
和声明用于修改属性的方法来组成新的ModifiableConnection
类。 eg 例如
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.