Exemplo n.º 1
0
 def test_delete_attr(self):
     db.session.add(models.User('User 1', '*****@*****.**'))
     db.session.add(models.Attribute('attr 1', 1))
     db.session.commit()
     rv = self.app.delete('/contacts/1/attributes/1')
     self.assertEqual(rv.status_code, 200)
     user = models.User.query.filter_by(name='User 1').first()
     self.assertEqual([], user.attributes)
def _after_registration_hook(sender, user, **extra):
    user_dir = os.path.join(basedir, 'data', str(user.id))
    if not os.path.exists(user_dir):
        os.makedirs(user_dir)

    attr_all = models.Attribute(all_attr, True, user.id)
    db.session.add(attr_all)
    db.session.commit()
    db.session.flush()
Exemplo n.º 3
0
 def test_edit_attr_wrong_input(self):
     db.session.add(models.User('User 1', '*****@*****.**'))
     db.session.add(models.Attribute('attr 1', 1))
     db.session.commit()
     rv = self.app.put('/contacts/1/attributes/1', data='{}')
     self.assertEqual(rv.status_code, 400)
     user = models.User.query.filter_by(name='User 1').first()
     attr = user.attributes[0]
     self.assertEqual('attr 1', attr.name)
def add_attribute():
    data = json.JSONDecoder().decode(request.data)
    if 'name' not in data:
        return make_response('', 400)

    attr = models.Attribute(data['name'], False, current_user.id)
    db.session.add(attr)
    try:
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        attr = models.Attribute.query.filter_by(name=data['name']).first()
        return make_response(json.dumps(attr.dict()), 409)

    return make_response(json.dumps(attr.dict()), 201)
def add_user():  # this adds a new contact for a logged-in user
    data = json.JSONDecoder().decode(request.data)
    if 'name' not in data or 'email' not in data:
        return make_response('', 400)  # error missing parameters

    user = models.Contact(data['name'], data['email'], current_user.id)
    db.session.add(user)
    db.session.commit()  # otherwise store contact persistently

    attr_id = models.Attribute(user.identity, False, current_user.id)
    attr_id.display_name = user.name
    attr_all = models.Attribute.query.filter_by(name=all_attr).first()
    db.session.add(attr_id)
    db.session.add(attr_all)
    db.session.commit()

    user.attributes.append(attr_id)  # assign identity attribute to contact
    user.attributes.append(attr_all)  # assign all attribtue to contact
    db.session.add(user)
    db.session.commit()

    aa_response = AttrAuth.add_user(user)  # inform AA about created user
    if aa_response is None:
        db.session.delete(user)  # delete user in error case
        db.session.commit()

        return make_response('', 500)

    user.secret_key = aa_response[
        'secretSeed']  # the AA currently responds with the secret seed not the secret key

    aa_response = AttrAuth.add_attr(
        user, attr_id, current_user)  # inform AA about identity AA
    if aa_response is None:
        db.session.delete(attr_id)  # delete it in error case
        db.session.commit()
        return make_response('', 500)

    aa_response = AttrAuth.add_attr(
        user, attr_all, current_user)  # inform AA about all attribute
    if aa_response is None:
        db.session.delete(attr_all)  # delete in error case
        db.session.commit()
        return make_response('', 500)

    return make_response(json.dumps(user.dict()), 201)
def assign_new_attribute(user_id):
    # to assign a new attribute to a contact
    user = models.Contact.query.filter_by(id=user_id).first_or_404()
    if user not in current_user.contacts:
        return make_response('', 403)

    data = json.JSONDecoder().decode(request.data)
    if 'name' not in data:
        return make_response('', 400)

    attr = models.Attribute(data['name'], False,
                            current_user.id)  # instantiate attribute
    db.session.add(attr)
    try:
        db.session.commit()  # try to commit
    except exc.IntegrityError:  # in case attribute already exists
        db.session.rollback()
        attr = models.Attribute.query.filter_by(
            display_name=data['name']).first()  # use the existent one
        if attr in user.attributes:  # if attribute already assigned - notify user
            return make_response('', 409)

    # else ...
    aa_response = AttrAuth.add_attr(user, attr, current_user)  # inform AA
    if aa_response is None:
        db.session.delete(attr)  # error case - delete attribute
        db.session.commit()
        return make_response('', 500)  # and inform user

    user.attributes.append(attr)  # otherwise assign it to contact
    db.session.add(user)
    db.session.commit()

    Policy.check_for(
        user, current_user
    )  # depending on the enforcement strategy - reevaluate all container

    return make_response(json.dumps(attr.dict()), 200)  # success
Exemplo n.º 7
0
 def test_show_attr(self):
     db.session.add(models.User('User 1', '*****@*****.**'))
     db.session.add(models.Attribute('attr 1',  1))
     db.session.commit()
     rv = self.app.get('/contacts/1/attributes/1')
     self.assertEqual(rv.status_code, 200)
Exemplo n.º 8
0
user4 = User(
    username='******',
    password='******',
    email='*****@*****.**',
    confirmed_at=datetime.datetime.now(),
    logged_in_edugain=False,
    active=True,
    pages='[{"widgets": [], "name": "My Dashboard", "slug": "my-dashboard"}]'
)
db.session.add(user4)

user5 = User(
    username='******',
    password='******',
    email='*****@*****.**',
    confirmed_at=datetime.datetime.now(),
    logged_in_edugain=False,
    active=True,
    pages='[{"widgets": [], "name": "My Dashboard", "slug": "my-dashboard"}]'
)
db.session.add(user5)

db.session.commit()

from um import models

attr_all = models.Attribute(all_attr, True, user.id,'')
db.session.add(attr_all)
db.session.commit()
#db.session.flush()
def edit_attribute(attr_id):
    # this tries to modify an attribute
    # all contacts that have the original attribute assigned are updated
    # therefore the original attribute is deleted from these
    # and the new attribute is assigned to these

    attr = models.Attribute.query.filter_by(id=attr_id).first_or_404()
    if attr not in current_user.attributes:
        return make_response('', 403)

    data = json.JSONDecoder().decode(request.data)
    if 'name' not in data:
        return make_response('', 400)

    attr_new = models.Attribute(data['name'], False, current_user.id)
    db.session.add(attr_new)
    try:
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        return make_response(json.dumps(attr.dict()), 409)

    users = models.Contact.query.filter(
        models.Contact.attributes.contains(attr)).all()
    delete_errors = list()
    create_errors = list()
    for user in users:  # iterate through contacts
        aa_response = AttrAuth.delete_attr(user, attr,
                                           current_user)  # delete wave
        if aa_response is None:
            delete_errors.append(
                user.id)  # in error case remember faulty contacts

        aa_response = AttrAuth.add_attr(user, data['name'],
                                        current_user)  # create wave
        if aa_response is None:
            create_errors.append(
                user.id)  # in error case remember faulty contacts

    if len(delete_errors) == 0 and len(
            create_errors) == 0:  # case no errors in wave
        db.session.delete(attr_new)
        db.session.commit()
        attr.name = attr_new.name
        db.session.add(attr)
        db.session.commit()
        return make_response(json.dumps(attr.dict()), 200)  # success

    if len(users) == len(
            create_errors):  # case create wave not successfull for any user
        db.session.delete(attr_new)  # delete the new attribute
        db.session.commit()  # at least state is consistent

    for user in users:  # iterate through users again
        if user.id not in delete_errors:
            user.attributes.remove(
                attr)  # delete old attribute for successful contacts
        if user.id not in create_errors:
            user.attributes.append(
                attr_new)  # create new attribute for successful contacts
        db.session.add(user)
    db.session.commit()

    # leave the rest as is
    # yet notify client with status code 500
    # to indicate an error occured
    return make_response('', 500)
db.session.commit()

# Contact 3 properties
p = models.Property('first_name', 'Steve', u3.id)
p1 = models.Property('last_name', 'Thriotree', u3.id)
p2 = models.Property('email', '*****@*****.**', u3.id)
p3 = models.Property('fon', '+1 555 303', u3.id)

db.session.add(p)
db.session.add(p1)
db.session.add(p2)
db.session.add(p3)
db.session.commit()

# Contacts attributes
a = models.Attribute('age')
a1 = models.Attribute('group')
a2 = models.Attribute('department')

u1.attributes.append(a)
u1.attributes.append(a1)
u1.attributes.append(a2)

u2.attributes.append(a)
u2.attributes.append(a2)

u3.attributes.append(a1)

db.session.add(u1)
db.session.add(u2)
db.session.add(u3)