better patch for unverified_context
[sfa.git] / tools / reset_gids.py
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3
4 import types
5
6 from sfa.storage.model import *
7 from sfa.storage.alchemy import *
8 from sfa.trust.gid import create_uuid
9 from sfa.trust.hierarchy import Hierarchy
10 from sfa.util.xrn import Xrn
11 from sfa.trust.certificate import Certificate, Keypair, convert_public_key
12
13 def fix_users():
14     s=global_dbsession
15     hierarchy = Hierarchy()
16     users = s.query(RegRecord).filter_by(type="user")
17     for record in users:
18         record.gid = ""
19         if not record.gid:
20             uuid = create_uuid()
21             pkey = Keypair(create=True)
22             pub_key=getattr(record,'reg_keys',None)
23             if len(pub_key) > 0:
24                 # use only first key in record
25                 if pub_key and isinstance(pub_key, types.ListType): pub_key = pub_key[0]
26                 pub_key = pub_key.key
27                 pkey = convert_public_key(pub_key)
28             urn = Xrn (xrn=record.hrn, type='user').get_urn()
29             email=getattr(record,'email',None)
30             gid_object = hierarchy.create_gid(urn, uuid, pkey, email = email)
31             gid = gid_object.save_to_string(save_parents=True)
32             record.gid = gid
33     s.commit()
34
35 if __name__ == '__main__':
36     fix_users()