def account_get_me(): namespace = 'object' ctype= 'account' #get the session (security) account = get_account() storage = get_storage(namespace=namespace) #try: logger.debug(" + Try to get '%s' ... " % account._id) record = storage.get(account._id, account=account) #logger.debug(" + Result: '%s'" % record) #except Exception, err: # self.logger.error("Exception !\nReason: %s" % err) # return HTTPError(404, _id+" Not Found") if record: data = record.dump(json=True) data['id'] = data['_id'] output = [data] reload_account(account._id,record) output={'total': 1, 'success': True, 'data': output} logger.debug(" + Output: "+str(output)) logger.debug('Response status: %s' % response.status) return output
def account_get_me(): namespace = 'object' ctype = 'account' #get the session (security) account = get_account() storage = get_storage(namespace=namespace) #try: logger.debug(" + Try to get '%s' ... " % account._id) record = storage.get(account._id, account=account) #logger.debug(" + Result: '%s'" % record) #except Exception, err: # self.logger.error("Exception !\nReason: %s" % err) # return HTTPError(404, _id+" Not Found") if record: data = record.dump(json=True) data['id'] = data['_id'] output = [data] reload_account(account._id, record) output = {'total': 1, 'success': True, 'data': output} logger.debug(" + Output: " + str(output)) logger.debug('Response status: %s' % response.status) return output
def account_update(_id=None): account = get_account() root_account = caccount(user="******", group="root") storage = get_storage(namespace='object',account=account) logger.debug("PUT:") data = request.body.readline() if not data: return HTTPError(400, "No data received") data = json.loads(data) if not isinstance(data,list): data = [data] for item in data: logger.debug(item) if '_id' in item: _id = item['_id'] del item['_id'] if 'id' in item: _id = item['id'] del item['id'] if not _id: return HTTPError(400, "No id recieved") try: record = caccount(storage.get(_id ,account=account)) logger.debug('Update account %s' % _id) except: logger.debug('Account %s not found' % _id) return HTTPError(404, "Account to update not found") #Get password if 'passwd' in item: logger.debug(' + Update password ...') record.passwd(str(item['passwd'])) del item['passwd'] #Get group if 'aaa_group' in item: logger.debug(' + Update group ...') record.chgrp(str(item['aaa_group'])) del item['aaa_group'] #get secondary groups if 'groups' in item: groups = [] for group in item['groups']: if group.find('group.') == -1: groups.append('group.%s' % group) else: groups.append(group) logger.debug(' + Update groups ...') logger.debug(' + Old groups : %s' % str(record.groups)) logger.debug(' + New groups : %s' % str(groups)) record.groups = groups del item['groups'] for _key in item: logger.debug('Update %s with %s' % (str(_key),item[_key])) setattr(record,_key,item[_key]) storage.put(record,account=account) #if user is itself, reload account if account._id == record._id: #user itself, reload reload_account(record._id)
def account_update(_id=None): account = get_account() root_account = caccount(user="******", group="root") storage = get_storage(namespace='object', account=account) logger.debug("PUT:") data = request.body.readline() if not data: return HTTPError(400, "No data received") data = json.loads(data) if not isinstance(data, list): data = [data] for item in data: logger.debug(item) if '_id' in item: _id = item['_id'] del item['_id'] if 'id' in item: _id = item['id'] del item['id'] if not _id: return HTTPError(400, "No id recieved") try: record = caccount(storage.get(_id, account=account)) logger.debug('Update account %s' % _id) except: logger.debug('Account %s not found' % _id) return HTTPError(404, "Account to update not found") #Get password if 'passwd' in item: logger.debug(' + Update password ...') record.passwd(str(item['passwd'])) del item['passwd'] #Get group if 'aaa_group' in item: logger.debug(' + Update group ...') record.chgrp(str(item['aaa_group'])) del item['aaa_group'] #get secondary groups if 'groups' in item: groups = [] for group in item['groups']: if group.find('group.') == -1: groups.append('group.%s' % group) else: groups.append(group) logger.debug(' + Update groups ...') logger.debug(' + Old groups : %s' % str(record.groups)) logger.debug(' + New groups : %s' % str(groups)) record.groups = groups del item['groups'] for _key in item: logger.debug('Update %s with %s' % (str(_key), item[_key])) setattr(record, _key, item[_key]) storage.put(record, account=account) #if user is itself, reload account if account._id == record._id: #user itself, reload reload_account(record._id)
def account_post(): #get the session (security) account = get_account() if not check_group_rights(account,group_managing_access): return HTTPError(403, 'Insufficient rights') root_account = caccount(user="******", group="root") storage = get_storage(namespace='object',account=account) logger.debug("POST:") data = request.body.readline() if not data: return HTTPError(400, "No data received") data = json.loads(data) ## Clean data try: del data['_id'] del data['id'] del data['crecord_type'] except: pass if data['user']: #check if already exist update = False _id = "account." + str(data['user']) try: record = storage.get(_id ,account=account) logger.debug('Update account %s' % _id) update = True except: logger.debug('Create account %s' % _id) #-----------------------UPDATE---------------------- if update: #Get password if data['passwd']: passwd = str(data['passwd']) else: passwd = None #Get group group = str(data['aaa_group']) if group: if group.find('group.') == -1: group = 'group.%s' % group #get secondary groups groups = data['groups'] secondary_groups = [] if groups: if not isinstance(groups,list): groups = [groups] for one_group in groups: if one_group.find('group.') == -1: one_group = 'group.%s' % one_group try : secondary_groups.append(cgroup(storage.get(one_group,account=account))) except Exception,err: logger.error('Error while searching secondary group: %s',err) #clean secondary groups for one_record in record.data['groups']: if unicode(one_record) not in secondary_groups: remove_account_from_group(one_record,record._id) #get clean account record = storage.get(_id ,account=account) #clean del data['passwd'] del data['aaa_group'] del data['groups'] #new record for key in dict(data).keys(): record.data[key] = data[key] update_account = caccount(record) #updating if passwd: logger.debug(' + Update password ...') update_account.passwd(passwd) if group: logger.debug(' + Update group ...') update_account.chgrp(group) if secondary_groups: logger.debug(' + Update groups ...') update_account.add_in_groups(secondary_groups) storage.put(update_account, account=account) storage.put(secondary_groups, account=account) reload_account(update_account._id) else: #----------------------------CREATION-------------------------- logger.debug(' + New account') new_account = caccount(user=data['user'], group=data['aaa_group'], lastname=data['lastname'], firstname=data['firstname'], mail=data['mail']) #passwd passwd = data['passwd'] new_account.passwd(passwd) logger.debug(" + Passwd: '%s'" % passwd) #secondary groups groups = data['groups'] secondary_groups = [] if groups: if not isinstance(groups,list): groups = [groups] for one_group in groups: if one_group.find('group.') == -1: one_group = 'group.%s' % one_group try : secondary_groups.append(cgroup(storage.get(one_group,account=account))) except Exception,err: logger.error('Error while searching secondary group: %s',err) new_account.add_in_groups(secondary_groups) storage.put(secondary_groups) #put record logger.debug(' + Save new account') new_account.chown(new_account._id) storage.put(new_account, account=account) #get rootdir logger.debug(' + Create view directory') rootdir = storage.get('directory.root', account=root_account) if rootdir: userdir = crecord({'_id': 'directory.root.%s' % new_account.user,'id': 'directory.root.%s' % new_account.user ,'expanded':'true'}, type='view_directory', name=new_account.user) userdir.chown(new_account._id) userdir.chgrp(new_account.group) userdir.chmod('g-w') userdir.chmod('g-r') storage.put(userdir, account=account) rootdir.add_children(userdir) storage.put(rootdir, account=root_account) storage.put(userdir, account=account) else: logger.error('Impossible to get rootdir')