def create(self, username, password, completename=None): """ Init the user model, and save it in DB """ if User.query.filter_by(nickname=username).count() != 0: return False password = encrypt_password(password) user_datastore.create_user(nickname=username, password=password, completename=completename, active=False) myuser = User.query.filter_by(nickname=username).first() # TODO : manage API key with flask-login apikey_seed = str(random.randint(0, 0xFFFFFFFFFFFFFFFF)) apikey_seed = apikey_seed + str(int(time.time())) apikey_seed = apikey_seed + sha256(username).hexdigest() apikey_seed = apikey_seed + sha256(password).hexdigest() apikey_seed = ''.join(random.sample(apikey_seed, len(apikey_seed))) myuser.api_key = sha256(apikey_seed).hexdigest() myuser.theme = "default" # the first user is active and admin if User.query.count() == 1: role = user_datastore.find_or_create_role("admin", description="Administrator") if role is not None: user_datastore.add_role_to_user(myuser, role) else: app.logger.error("Cannot find and affect admin role to user") user_datastore.activate_user(myuser) db.session.commit() return True
def create(self, username, password, completename=None): """ Init the user model, and save it in DB """ if User.query.filter_by(nickname=username).count() != 0: return False password = encrypt_password(password) user_datastore.create_user(nickname=username, password=password, completename=completename, active=False) myuser = User.query.filter_by(nickname=username).first() # TODO : manage API key with flask-login apikey_seed = str(random.randint(0, 0xFFFFFFFFFFFFFFFF)) apikey_seed = apikey_seed + str(int(time.time())) apikey_seed = apikey_seed + sha256(username).hexdigest() apikey_seed = apikey_seed + sha256(password).hexdigest() apikey_seed = ''.join(random.sample(apikey_seed, len(apikey_seed))) myuser.api_key = sha256(apikey_seed).hexdigest() myuser.theme = "default" # the first user is active and admin if User.query.count() == 1: role = user_datastore.find_or_create_role( "admin", description="Administrator") if role is not None: user_datastore.add_role_to_user(myuser, role) else: app.logger.error("Cannot find and affect admin role to user") user_datastore.activate_user(myuser) db.session.commit() return True
def manage_admin_role(cls, uid): user = user_datastore.get_user(int(uid)) role = user_datastore.find_or_create_role("admin", description="Administrator") if role is not None: if role not in user.roles: app.logger.debug("Giving admin privileges to user %s" % (user.nickname)) user_datastore.add_role_to_user(user, role) else: app.logger.debug("Removing admin privileges to user %s" % (user.nickname)) user_datastore.remove_role_from_user(user, role) else: app.logger.error("Cannot find and affect admin role to user") return False db.session.commit() return True
def manage_admin_role(cls, uid): """ Toggle admin roles for given uid """ user = user_datastore.get_user(int(uid)) role = user_datastore.find_or_create_role( "admin", description="Administrator") if role is not None: if role not in user.roles: app.logger.debug("Giving admin privileges to user %s" % (user.nickname)) user_datastore.add_role_to_user(user, role) else: app.logger.debug("Removing admin privileges to user %s" % (user.nickname)) user_datastore.remove_role_from_user(user, role) else: app.logger.error("Cannot find and affect admin role to user") return False db.session.commit() return True
def add_role_to_user(uid, role): user = user_datastore.get_user(int(uid)) user_datastore.add_role_to_user(user, role)