Revisión 80
Añadido por Manu Mora Gordillo hace alrededor de 14 años
controlies/Utils/LdapUtils.py | ||
---|---|---|
##############################################################################
|
||
# -*- coding: utf-8 -*-
|
||
# Project: ControlIES
|
||
# Module: LdapUtils.py
|
||
# Purpose: Ldap utils
|
||
# Language: Python 2.5
|
||
# Date: 18-Feb-2011.
|
||
# Ver: 18-Feb-2011.
|
||
# Author: Manuel Mora Gordillo
|
||
# Francisco Mendez Palma
|
||
# Copyright: 2011 - Manuel Mora Gordillo <manuito @no-spam@ gmail.com>
|
||
# 2011 - Francisco Mendez Palma <fmendezpalma @no-spam@ gmail.com>
|
||
#
|
||
# ControlIES is free software: you can redistribute it and/or modify
|
||
# it under the terms of the GNU General Public License as published by
|
||
# the Free Software Foundation, either version 3 of the License, or
|
||
# (at your option) any later version.
|
||
# ControlIES is distributed in the hope that it will be useful,
|
||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
# GNU General Public License for more details.
|
||
# You should have received a copy of the GNU General Public License
|
||
# along with ControlIES. If not, see <http://www.gnu.org/licenses/>.
|
||
#
|
||
##############################################################################
|
||
|
||
# Get all users
|
||
def getAllUsers(ldap):
|
||
result = ldap.search("ou=People","uid=*",["uid","cn"])
|
||
|
||
rows = []
|
||
for u in result:
|
||
rows.append([u[0][1]['uid'][0] , u[0][1]['cn'][0] ]);
|
||
|
||
return rows
|
||
|
||
|
||
# Get all groups classified by type
|
||
def getAllGroups(ldap):
|
||
result = ldap.search("ou=Group","(|(groupType=school_department)(groupType=school_class))",["cn","groupType"])
|
||
|
||
departments = []
|
||
classrooms = []
|
||
for g in result:
|
||
if g[0][1]["groupType"][0] == "school_department":
|
||
departments.append(g[0][1]["cn"][0])
|
||
elif g[0][1]["groupType"][0] == "school_class":
|
||
classrooms.append(g[0][1]["cn"][0])
|
||
|
||
departments.sort()
|
||
classrooms.sort()
|
||
|
||
return { "departments":departments, "classrooms":classrooms }
|
||
|
||
|
||
# Get the max ID of the groups and users
|
||
def getMaxID(ldap):
|
||
result = ldap.search("ou=Group","cn=*",["gidNumber"])
|
||
|
||
numbers = []
|
||
for i in result:
|
||
numbers.append(int(i[0][1]['gidNumber'][0]))
|
||
|
||
result = ldap.search("ou=People","uid=*",["gidNumber","uidNumber"])
|
||
for i in result:
|
||
numbers.append(int(i[0][1]['gidNumber'][0]))
|
||
numbers.append(int(i[0][1]['uidNumber'][0]))
|
||
|
||
numbers.sort()
|
||
|
||
maxID = 1
|
||
if len(numbers) > 0:
|
||
maxID = numbers[len(numbers)-1] + 1
|
||
|
||
return maxID
|
||
|
||
def whatHome(type):
|
||
|
||
if type == "teacher":
|
||
return "/home/profesor/"
|
||
else:
|
||
return "/home/alumnos/"
|
controlies/Plugins/Groups.py | ||
---|---|---|
import logging
|
||
from math import ceil
|
||
from operator import itemgetter
|
||
from Utils import Utils
|
||
from Utils import Utils, LdapUtils
|
||
|
||
class Groups(object):
|
||
|
||
... | ... | |
result = sorted(rows, key=itemgetter(sortBy), reverse=reverseSort)
|
||
|
||
return { "page":page, "total":totalPages, "records":len(search), "rows":result[start:finish] }
|
||
|
||
def getAllGroups(self):
|
||
result = self.ldap.search("ou=Group","(|(groupType=school_department)(groupType=school_class))",["cn","groupType"])
|
||
|
||
departments = []
|
||
classrooms = []
|
||
for g in result:
|
||
if g[0][1]["groupType"][0] == "school_department":
|
||
departments.append(g[0][1]["cn"][0])
|
||
elif g[0][1]["groupType"][0] == "school_class":
|
||
classrooms.append(g[0][1]["cn"][0])
|
||
|
||
departments.sort()
|
||
classrooms.sort()
|
||
return { "departments":departments, "classrooms":classrooms }
|
||
|
||
def buildFilter(self, args):
|
||
filter = "(&(cn=*)(|(groupType=school_class)(groupType=school_department))"
|
||
... | ... | |
|
||
|
||
def add(self):
|
||
maxID = str(self.getMaxID())
|
||
maxID = str(LdapUtils.getMaxID(self.ldap))
|
||
|
||
members = []
|
||
for m in self.users.split(','):
|
||
... | ... | |
return "OK"
|
||
|
||
|
||
def getMaxID(self): # find the maximum ID
|
||
result = self.ldap.search("ou=Group","cn=*",["gidNumber"])
|
||
numbers = []
|
||
for i in result:
|
||
numbers.append(int(i[0][1]['gidNumber'][0]))
|
||
|
||
result = self.ldap.search("ou=People","uid=*",["gidNumber","uidNumber"])
|
||
for i in result:
|
||
numbers.append(int(i[0][1]['gidNumber'][0]))
|
||
numbers.append(int(i[0][1]['uidNumber'][0]))
|
||
|
||
numbers.sort()
|
||
|
||
maxID = 1
|
||
if len(numbers) > 0:
|
||
maxID = numbers[len(numbers)-1] + 1
|
||
|
||
return maxID
|
||
|
||
def getGroupData(self):
|
||
|
||
result = self.ldap.search("ou=Group","cn="+self.name,["cn","grouptype","memberuid"])
|
controlies/Plugins/Users.py | ||
---|---|---|
import hashlib
|
||
from math import ceil
|
||
from operator import itemgetter
|
||
from Utils import Utils, LdapUtils
|
||
|
||
class Users(object):
|
||
|
||
... | ... | |
|
||
def add(self):
|
||
|
||
maxID = str(self.getMaxID())
|
||
maxID = str(LdapUtils.getMaxID(self.ldap))
|
||
passwd = hashlib.sha1(self.password).hexdigest()
|
||
|
||
attr = [
|
||
... | ... | |
('uidnumber', [maxID] ),
|
||
('gidnumber', [maxID] ),
|
||
('loginshell', ['/bin/bash'] ),
|
||
('homeDirectory', [self.whatHome() + self.user] ),
|
||
('homeDirectory', [LdapUtils.whatHome(self.type) + self.user] ),
|
||
#('jpegPhoto', ['jpegPhoto'] ),
|
||
('userpassword', [passwd])
|
||
]
|
||
... | ... | |
return "OK"
|
||
|
||
|
||
def getMaxID(self): # find the maximum ID
|
||
result = self.ldap.search("ou=Group","cn=*",["gidNumber"])
|
||
numbers = []
|
||
for i in result:
|
||
numbers.append(int(i[0][1]['gidNumber'][0]))
|
||
|
||
result = self.ldap.search("ou=People","uid=*",["gidNumber","uidNumber"])
|
||
for i in result:
|
||
numbers.append(int(i[0][1]['gidNumber'][0]))
|
||
numbers.append(int(i[0][1]['uidNumber'][0]))
|
||
|
||
numbers.sort()
|
||
|
||
maxID = 1
|
||
if len(numbers) > 0:
|
||
maxID = numbers[len(numbers)-1] + 1
|
||
|
||
return maxID
|
||
|
||
|
||
def existsUsername(self):
|
||
|
||
result = self.ldap.search("ou=People","uid="+self.user,["uid"])
|
||
... | ... | |
|
||
return dataUser
|
||
|
||
def whatHome(self):
|
||
|
||
if self.type == "teacher":
|
||
return "/home/profesor/"
|
||
else:
|
||
return "/home/alumnos/"
|
||
|
||
def getAllUsers(self):
|
||
result = self.ldap.search("ou=People","uid=*",["uid","cn"])
|
||
|
||
rows = []
|
||
for u in result:
|
||
rows.append([u[0][1]['uid'][0] , u[0][1]['cn'][0] ]);
|
||
|
||
return rows
|
controlies/MainLoop.py | ||
---|---|---|
import os
|
||
import simplejson as json
|
||
from Plugins.LdapConnection import LdapConnection, ILdapConnection
|
||
from Utils import LdapUtils
|
||
|
||
class ControlIESProtocol(resource.Resource):
|
||
|
||
... | ... | |
return json.dumps({"response" : response})
|
||
|
||
elif request.args['action'][0] == "getAllUsers":
|
||
u = Users(l,"","","","","","","","","")
|
||
response = u.getAllUsers()
|
||
response = LdapUtils.getAllUsers(l)
|
||
return json.dumps(response)
|
||
|
||
elif request.args['action'][0] == "delete":
|
||
... | ... | |
return json.dumps(response)
|
||
|
||
elif request.args['action'][0] == "getAllGroups":
|
||
g = Groups(l,"","","")
|
||
response = g.getAllGroups()
|
||
response = LdapUtils.getAllGroups(l)
|
||
return json.dumps(response)
|
||
|
||
elif request.args['action'][0] == "getGroupData":
|
Exportar a: Unified diff
Añadiendo LdapUtils