admin_dao: add the get_admin_entity function

parent b2d8a0bb
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Avencall
# Copyright (C) 2015-2016 Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -18,6 +18,7 @@
from sqlalchemy import and_
from xivo_dao.alchemy.user import User
from xivo_dao.alchemy.entity import Entity
from xivo_dao.helpers.db_manager import daosession
......@@ -30,6 +31,14 @@ def check_username_password(session, username, password):
return row is not None
@daosession
def get_admin_entity(session, username):
filter_ = and_(
User.login == username,
User.valid == 1,
)
return session.query(Entity.name).join(User).filter(filter_).scalar()
@daosession
def get_admin_id(session, username):
return session.query(User.id).filter(and_(User.login == username,
......
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Avencall
# Copyright (C) 2015-2016 Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -57,3 +57,18 @@ class TestAdminUserDAO(DAOTestCase):
result = admin_dao.get_admin_id('foo')
assert_that(result, equal_to(admin.id))
def test_get_admin_entity(self):
entity = self.add_entity(name='test')
admin = self.add_admin(login='foo', passwd='bar', entity_id=entity.id)
result = admin_dao.get_admin_entity('foo')
assert_that(result, equal_to('test'))
def test_get_admin_entity_no_entify(self):
admin = self.add_admin(login='alice', passwd='foobar')
result = admin_dao.get_admin_entity('alice')
assert_that(result, equal_to(None))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment