root/controlies/Plugins/Groups.py @ 89
61 | manumora | ##############################################################################
|
|
# -*- coding: utf-8 -*-
|
|||
# Project: ControlIES
|
|||
# Module: Groups.py
|
|||
# Purpose: Groups class
|
|||
# Language: Python 2.5
|
|||
# Date: 7-Feb-2011.
|
|||
# Ver: 7-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
|
|||
76 | manumora | # along with ControlIES. If not, see <http://www.gnu.org/licenses/>.
|
|
61 | manumora | #
|
|
##############################################################################
|
|||
import ldap
|
|||
import logging
|
|||
from math import ceil
|
|||
from operator import itemgetter
|
|||
80 | manumora | from Utils import Utils, LdapUtils
|
|
61 | manumora | ||
class Groups(object):
|
|||
def __init__(self):
|
|||
pass
|
|||
76 | manumora | def __init__(self,ldap,type,name,users):
|
|
61 | manumora | self.ldap = ldap
|
|
69 | manumora | self.type = type
|
|
79 | manumora | self.name = Utils.parseToLdap(name)
|
|
76 | manumora | self.users = users
|
|
79 | manumora | ||
69 | manumora | def validation(self,action):
|
|
77 | manumora | if self.type == "none":
|
|
return "type"
|
|||
69 | manumora | ||
61 | manumora | if self.name == "":
|
|
return "name"
|
|||
76 | manumora | ||
if self.users == "":
|
|||
return "users"
|
|||
61 | manumora | return "OK"
|
|
def process(self,action):
|
|||
if action == "add":
|
|||
69 | manumora | val = self.validation(action)
|
|
61 | manumora | ||
if val != "OK":
|
|||
return val
|
|||
else:
|
|||
response = self.add()
|
|||
return response
|
|||
if action == "modify":
|
|||
69 | manumora | val = self.validation(action)
|
|
76 | manumora | ||
61 | manumora | if val != "OK":
|
|
return val
|
|||
else:
|
|||
response = self.modify()
|
|||
69 | manumora | return response
|
|
61 | manumora | ||
def list(self,args):
|
|||
filter = self.buildFilter(args)
|
|||
79 | manumora | search = self.ldap.search("ou=Group",filter,["cn","gidNumber","groupType","memberUid"])
|
|
61 | manumora | ||
# grid parameters
|
|||
limit = int(args['rows'][0])
|
|||
page = int(args['page'][0])
|
|||
start = limit * page - limit
|
|||
finish = start + limit;
|
|||
# sort by field
|
|||
sortBy = args['sidx'][0]
|
|||
if sortBy == "cn":
|
|||
sortBy = "id"
|
|||
# reverse Sort
|
|||
reverseSort = False
|
|||
if args['sord'][0] == "asc":
|
|||
reverseSort = True
|
|||
# type of group (Classroom/Department)
|
|||
try:
|
|||
searchType = args['type'][0]
|
|||
except LookupError:
|
|||
searchType = "none"
|
|||
rows = []
|
|||
for i in search:
|
|||
typeRow="Aula"
|
|||
if i[0][1]["groupType"][0]=="school_department":
|
|||
typeRow="Departamento"
|
|||
if searchType == typeRow or searchType=="none":
|
|||
79 | manumora | ||
try:
|
|||
usersNumber = len(i[0][1]["memberUid"])
|
|||
except:
|
|||
usersNumber = 0
|
|||
61 | manumora | row = {
|
|
69 | manumora | "id":i[0][1]["cn"][0],
|
|
79 | manumora | "cell":[typeRow, i[0][1]["cn"][0], i[0][1]["gidNumber"][0], usersNumber],
|
|
61 | manumora | "type": typeRow,
|
|
"cn": i[0][1]["cn"][0],
|
|||
79 | manumora | "gidNumber": i[0][1]["gidNumber"][0],
|
|
"usersNumber": usersNumber
|
|||
61 | manumora | }
|
|
rows.append(row)
|
|||
if len(search) > 0:
|
|||
totalPages = ceil( len(search) / int(limit) )
|
|||
else:
|
|||
totalPages = 0
|
|||
if page > totalPages:
|
|||
page = totalPages
|
|||
# sort rows
|
|||
result = sorted(rows, key=itemgetter(sortBy), reverse=reverseSort)
|
|||
return { "page":page, "total":totalPages, "records":len(search), "rows":result[start:finish] }
|
|||
62 | manumora | ||
61 | manumora | ||
def buildFilter(self, args):
|
|||
filter = "(&(cn=*)(|(groupType=school_class)(groupType=school_department))"
|
|||
try:
|
|||
filter = filter + "(cn=*" + args['cn'][0] + "*)"
|
|||
except LookupError:
|
|||
pass
|
|||
try:
|
|||
filter = filter + "(gidNumber=" + args['gidNumber'][0] + ")"
|
|||
except LookupError:
|
|||
pass
|
|||
filter = filter + ")"
|
|||
return filter
|
|||
def add(self):
|
|||
80 | manumora | maxID = str(LdapUtils.getMaxID(self.ldap))
|
|
69 | manumora | ||
76 | manumora | members = []
|
|
for m in self.users.split(','):
|
|||
members.append("uid=" + m + ",ou=People,dc=instituto,dc=extremadura,dc=es")
|
|||
69 | manumora | attr = [
|
|
('objectclass', ['top','posixGroup','lisGroup','lisAclGroup']),
|
|||
('grouptype', [self.type] ),
|
|||
('gidnumber', [maxID] ),
|
|||
61 | manumora | ('cn', [self.name] ),
|
|
69 | manumora | ('description', [self.name+' department group']),
|
|
76 | manumora | ('memberuid', self.users.split(',')),
|
|
('member', members)
|
|||
61 | manumora | ]
|
|
69 | manumora | ||
self.ldap.add("cn="+self.name+",ou=Group", attr)
|
|||
61 | manumora | ||
return "OK"
|
|||
76 | manumora | ||
61 | manumora | ||
def modify(self):
|
|||
77 | manumora | ||
members = []
|
|||
for m in self.users.split(','):
|
|||
members.append("uid=" + m + ",ou=People,dc=instituto,dc=extremadura,dc=es")
|
|||
61 | manumora | mod_attrs = [
|
|
77 | manumora | (ldap.MOD_REPLACE, 'memberuid', self.users.split(',')),
|
|
(ldap.MOD_REPLACE, 'member', members)
|
|||
61 | manumora | ]
|
|
79 | manumora | ||
77 | manumora | self.ldap.modify('cn='+ self.name +',ou=Group', mod_attrs)
|
|
61 | manumora | ||
77 | manumora | return "OK"
|
|
76 | manumora | ||
79 | manumora | ||
69 | manumora | def delete(self):
|
|
self.ldap.delete('cn='+ self.name +',ou=Group')
|
|||
79 | manumora | return "OK"
|
|
69 | manumora | ||
77 | manumora | def getGroupData(self):
|
|
76 | manumora | ||
77 | manumora | result = self.ldap.search("ou=Group","cn="+self.name,["cn","grouptype","memberuid"])
|
|
78 | manumora | members = result[0][0][1]["memberUid"]
|
|
members.sort()
|
|||
77 | manumora | dataGroup = {
|
|
"name":result[0][0][1]["cn"][0],
|
|||
"type":result[0][0][1]["groupType"][0],
|
|||
78 | manumora | "memberuid":members
|
|
77 | manumora | }
|
|
return dataGroup
|
|||
76 | manumora | def getGroupUsers(self):
|
|
result = self.ldap.search("ou=Group","cn="+self.name,["cn","memberUid"])
|
|||
members = []
|
|||
for m in result:
|
|||
members.append(m[0][1]["memberUid"][0])
|
|||
members.sort()
|
|||
return members
|