Example #1
0
def create(username, password=None, access_id=None, externally_managed=False):
    """
    This function will create an operator record in the database.
    
    :rtype: :class:`ensconce.model.Operator`
    """
    check = get_by_username(username, assert_exists=False)
    # First, check to see if the given username exists.
    if check:
        raise ValueError("User already exists: {0}".format(username))

    # Force the password to be null if it is empty (prevent logins w/ empty password)
    if password == "":
        password = None

    session = meta.Session()
    try:
        operator = model.Operator()
        operator.username = username
        if password is not None:
            operator.password = pwhash.obscure(password)
        operator.access_id = access_id
        operator.externally_managed = externally_managed
        session.add(operator)
        session.flush()
    except:
        log.exception("Error saving new operator_id.")
        raise

    return operator
Example #2
0
 def _create_operators(self):
     session = meta.Session()
     for username, pw in (('op1', 'pw1'), ('op2', 'pw2'), ('op3', 'pw3')):        
         o = model.Operator()
         o.username = username
         o.password = pwhash.obscure(pw)
         o.access_id = 1 # FIXME: Probably should be creating access levels in data pouplator 
         session.add(o)
         self.operators[o.username] = o
         session.flush()
         log.debug("Created {0}".format(o))
Example #3
0
def set_entity_attributes(entity, update_attributes, encrypted_attributes=None, hashed_attributes=None):
    """
    A convenience method to set attributes (column values) on an entity and 
    return the attributes which were actually modified.
    
    :param obj: The entity object being updated.
    :param update_attributes: A dict of attributes that should be set on this object.
    :param encrypted_attributes: A list of any attributes that are encrypted (and need
                            to be set using the *_decrypted method variant).
    :return: A list of modified attributes (column names).
    :rtype: list
    """
    if encrypted_attributes is None:
        encrypted_attributes = []
    
    if hashed_attributes is None:
        hashed_attributes = []
        
    for (attrib, value) in update_attributes.items():
        if attrib not in encrypted_attributes and attrib not in hashed_attributes: 
            setattr(entity, attrib, value)
    
    for attrib in encrypted_attributes:
        if attrib in update_attributes:
            value = update_attributes[attrib]
            decrypted_attrib = attrib + '_decrypted'
            if value != getattr(entity, decrypted_attrib):
                setattr(entity, decrypted_attrib, value)

    for attrib in hashed_attributes:
        if attrib in update_attributes:
            value = update_attributes[attrib]
            if value is None:
                setattr(entity, attrib, value)
            elif not pwhash.compare(getattr(entity, attrib), value):
                setattr(entity, attrib, pwhash.obscure(value)) # Using default hashtype
                
    # Use SQLAlchemy's cool get_history method:
    # http://docs.sqlalchemy.org/en/rel_0_7/orm/session.html?highlight=get_history#sqlalchemy.orm.attributes.History
    modified = []
    for attrib in update_attributes.keys():
        hist = attributes.get_history(entity, attrib)
        if hist.has_changes():
            modified.append(attrib)
    
    return modified