Skip to content

Commit

Permalink
Merge pull request #197 from elg/master
Browse files Browse the repository at this point in the history
Add databases models infos for future developpements
  • Loading branch information
elg committed Oct 23, 2017
2 parents 1d5eda9 + 8679605 commit 9e5197d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
23 changes: 23 additions & 0 deletions passhportd/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ class Target_User(db.Model):
db.ForeignKey("user.id"),
primary_key=True)

class Tg_admins(db.Model):
"""Users authorized to admin targetgroups"""
__tablename__ = "tg_admins"
target_id = db.Column(
db.Integer,
db.ForeignKey("targetgroup.id"),
primary_key=True)
user_id = db.Column(
db.Integer,
db.ForeignKey("user.id"),
primary_key=True)

class Ug_admins(db.Model):
"""Users authorized to admin usergroups"""
__tablename__ = "ug_admins"
target_id = db.Column(
db.Integer,
db.ForeignKey("usergroup.id"),
primary_key=True)
user_id = db.Column(
db.Integer,
db.ForeignKey("user.id"),
primary_key=True)

class Group_User(db.Model):
"""Groupuser users in groups"""
Expand Down
9 changes: 8 additions & 1 deletion passhportd/app/models_mod/target/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Target(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(256), index=True, unique=True)
hostname = db.Column(db.String(120), index=True, nullable=False)
servertype = db.Column(db.String(120), index=True, server_default="ssh")
login = db.Column(db.String(120), index=True)
port = db.Column(db.Integer, index=False)
sshoptions = db.Column(db.String(500), index=True)
Expand All @@ -29,9 +30,9 @@ class Target(db.Model):
def __repr__(self):
"""Return main data of the target as a string"""
output = []

output.append("Name: {}".format(self.name))
output.append("Hostname: {}".format(self.hostname))
output.append("Server Type : {}".format(self.servertype))
output.append("Login: {}".format(self.login))
output.append("Port: {}".format(str(self.port)))
output.append("SSH options: {}".format(self.sshoptions))
Expand All @@ -53,6 +54,7 @@ def simplejson(self):

output = output + "\"Name\": \"" + format(self.name) + "\",\n"
output = output + "\"Hostname\": \"" + format(self.hostname) + "\",\n"
output = output + "\"Server Type\": \"" + format(self.servertype) + "\",\n"
output = output + "\"Login\": \"" + format(self.login) + "\",\n"
output = output + "\"Port\": \"" + format(self.port) + "\",\n"
output = output + "\"Comment\": \"" + format(self.comment) + "\"\n"
Expand All @@ -71,6 +73,11 @@ def show_hostname(self):
return self.hostname


def show_servertype(self):
"""Return a string containing the target's servertype"""
return self.servertype


def show_comment(self):
"""Return a string containing the target's comment"""
return self.comment
Expand Down
3 changes: 2 additions & 1 deletion passhportd/app/models_mod/targetgroup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class Targetgroup(db.Model):
primaryjoin=id == tgroup_of_tgroup.c.container_id,
secondaryjoin=id == tgroup_of_tgroup.c.subtargetgroup_id,
backref="containedin")

# Admins - can admin usergroups and targetgroups (add and remove users)
tgadmins = db.relationship("User", secondary="tg_admins")

def __repr__(self):
"""Return main data of the targetgroup as a string"""
Expand Down
5 changes: 4 additions & 1 deletion passhportd/app/models_mod/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class User(db.Model):
nullable=False)
comment = db.Column(db.String(500), index=True)

# Relations
# Relations (in targetgroups)
targets = db.relationship("Target", secondary="target_user")
# Admins - can admin usergroups and targetgroups (add and remove users)
adminoftg = db.relationship("Targetgroup", secondary="tg_admins")
adminofug = db.relationship("Usergroup", secondary="ug_admins")


def __repr__(self):
Expand Down
3 changes: 2 additions & 1 deletion passhportd/app/models_mod/usergroup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class Usergroup(db.Model):
primaryjoin=id == group_of_group.c.container_id,
secondaryjoin=id == group_of_group.c.subgroup_id,
backref="containedin")

# Admins
ugadmins = db.relationship("User", secondary="ug_admins")

def __repr__(self):
"""Return main data of the usergroup as a string"""
Expand Down
9 changes: 8 additions & 1 deletion passhportd/app/views_mod/target/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ def target_create():
# Simplification for the reading
name = request.form["name"]
hostname = request.form["hostname"]
#servertype = request.form["servertype"]
servertype = "ssh"
login = request.form["login"]
port = request.form["port"]
sshoptions = request.form["sshoptions"]
Expand All @@ -218,6 +220,9 @@ def target_create():
return "ERROR: The name and hostname are required ", 417, \
{"content-type": "text/plain; charset=utf-8"}

if not servertype:
servertype = "ssh"

if not login:
login = "root"

Expand All @@ -236,6 +241,7 @@ def target_create():
t = target.Target(
name=name,
hostname=hostname,
servertype=servertype,
login=login,
port=port,
sshoptions=sshoptions,
Expand Down Expand Up @@ -265,6 +271,7 @@ def target_edit():
name = request.form["name"]
new_name = request.form["new_name"]
new_hostname = request.form["new_hostname"]
#new_servertype = request.form["new_servertype"]
new_login = request.form["new_login"]
new_port = request.form["new_port"]
new_sshoptions = request.form["new_sshoptions"]
Expand All @@ -289,7 +296,7 @@ def target_edit():
# Let's modify only relevent fields
if new_login:
to_update.update({"login": new_login})

if new_sshoptions:
to_update.update({"sshoptions": new_sshoptions})

Expand Down
10 changes: 4 additions & 6 deletions passhportd/db_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
from config import SQLALCHEMY_MIGRATE_REPO


# From:
# http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
migration = SQLALCHEMY_MIGRATE_REPO + ("/versions/%03d_migration.py" % (v + 1))
version = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
migration = SQLALCHEMY_MIGRATE_REPO + ("/versions/%03d_migration.py" % (version + 1))
tmp_module = imp.new_module("old_model")
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
exec(old_model, tmp_module.__dict__)
Expand All @@ -26,6 +24,6 @@
db.metadata)
open(migration, "wt").write(script)
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
version = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print("New migration saved as " + migration)
print("Current database version: " + str(v))
print("Current database version: " + str(version))

0 comments on commit 9e5197d

Please sign in to comment.