summaryrefslogtreecommitdiff
path: root/coip/apps/userprofile/tasks.py
blob: 175c8a5202442a39e39e905149436bbeaad19589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
Created on Aug 18, 2011

@author: leifj
'''
from coip.settings import METADATA
from lxml import etree
from hashlib import sha1
from django.contrib.auth.models import User
from coip.apps.name.models import lookup
from coip.apps.userprofile.models import UserProfile
from coip.apps.membership.models import add_member
from celery.decorators import periodic_task
from celery.schedules import crontab

@periodic_task(run_every=crontab(hour="*", minute="*/3", day_of_week="*"))
def import_metadata():
    doc = etree.parse(METADATA)
    ns = {'md': 'urn:oasis:names:tc:SAML:2.0:metadata',
          'xml': 'http://www.w3.org/XML/1998/namespace'}
    for e in doc.xpath("md:EntityDescriptor",namespaces=ns):
        entityId = e.get('entityID')
        print entityId
        display = entityId
        x = e.xpath("md:OrganizationDisplayName",namespaces=ns)
        if x:
            display = x[0]    
        
        username = "entity:%s" % sha1(entityId).hexdigest()
        (user,created) = User.objects.get_or_create(username=username)
        save = created
        profile = user.get_profile()

        if created:
            anyuser = lookup("system:anyuser")
            anyentity = lookup("system:anyentity",True)
            anyentity.setacl(anyuser, "rl")
            profile.type = UserProfile.ENTITY
            profile.home = anyentity
            add_member(anyentity, user)
        
        if display != profile.display_name:
            profile.display_name = display
            save = True
            
        if save:
            user.save()
            profile.save()