From 292853fd5736b31a55e4030c348960beb2c35625 Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Tue, 26 Sep 2017 14:11:00 -0700 Subject: [PATCH 01/10] backported domain/manager sync --- admin/manage.py | 55 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/admin/manage.py b/admin/manage.py index 0ba3501f..095f6f72 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -33,7 +33,6 @@ def admin(localpart, domain_name, password): db.session.add(user) db.session.commit() - @manager.command def user(localpart, domain_name, password, hash_scheme='SHA512-CRYPT'): """ Create a user @@ -51,6 +50,14 @@ def user(localpart, domain_name, password, hash_scheme='SHA512-CRYPT'): db.session.add(user) db.session.commit() +@manager.command +def domain(domain_name, max_users=0, max_aliases=0, max_quota_bytes=0): + domain = models.Domain.query.get(domain_name) + if not domain: + domain = models.Domain(name=domain_name) + db.session.add(domain) + db.session.commit() + @manager.command def user_import(localpart, domain_name, password_hash, hash_scheme='SHA512-CRYPT'): """ Import a user along with password hash. Available hashes: @@ -78,7 +85,30 @@ def config_update(verbose=False, delete_objects=False): import yaml, sys new_config=yaml.load(sys.stdin) # print new_config - users=new_config['users'] + domains=new_config.get('domains',[]) + tracked_domains=set() + for domain_config in domains: + if verbose: + print(str(domain_config)) + domain_name = domain_config['name'] + max_users=domain_config.get('max_users',0) + max_aliases=domain_config.get('max_aliases',0) + max_quota_bytes=domain_config.get('max_quota_bytes',0) + tracked_domains.add(domain_name) + domain = models.Domain.query.get(domain_name) + if not domain: + domain = models.Domain(name=domain_name, + max_users=max_users, + max_aliases=max_aliases, + max_quota_bytes=max_quota_bytes) + db.session.add(domain) + else: + domain.max_users = max_users + domain.max_aliases = max_aliases + domain.max_quota_bytes = max_quota_bytes + db.session.add(domain) + + users=new_config.get('users',[]) tracked_users=set() for user_config in users: if verbose: @@ -94,6 +124,7 @@ def config_update(verbose=False, delete_objects=False): db.session.add(domain) user = models.User.query.get(email) tracked_users.add(email) + tracked_domains.add(domain_name) if not user: user = models.User( localpart=localpart, @@ -103,7 +134,7 @@ def config_update(verbose=False, delete_objects=False): user.set_password(password_hash, hash_scheme=hash_scheme, raw=True) db.session.add(user) - aliases=new_config['aliases'] + aliases=new_config.get('aliases',[]) tracked_aliases=set() for alias_config in aliases: if verbose: @@ -118,6 +149,7 @@ def config_update(verbose=False, delete_objects=False): db.session.add(domain) alias = models.Alias.query.get(email) tracked_aliases.add(email) + tracked_domains.add(domain_name) if not alias: alias = models.Alias( localpart=localpart, @@ -126,11 +158,23 @@ def config_update(verbose=False, delete_objects=False): email=email ) else: - if type(destination) == type(""): + if type(destination) == str: alias.destination = destination.split(',') else: alias.destination = destination db.session.add(alias) + + managers=new_config.get('managers',[]) + # tracked_managers=set() + for manager_config in managers: + if verbose: + print(str(manager_config)) + domain_name = manager_config['domain'] + user_name = manage_config['user'] + domain = models.Domain.query.get(domain_name) + manageruser = models.User.query.get(user_name + '@' + domain_name) + domain.managers.append(manageruser) + db.session.add(domain) if delete_objects: for user in db.session.query(models.User).all(): @@ -139,6 +183,9 @@ def config_update(verbose=False, delete_objects=False): for alias in db.session.query(models.Alias).all(): if not ( alias.email in tracked_aliases ): db.session.delete(alias) + for domain in db.session.query(models.Domain).all(): + if not ( domain.name in tracked_domains ): + db.session.delete(domain) db.session.commit() @manager.command From 9f267a44ee82148f1461a115023ac8eabf8bbbac Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Tue, 26 Sep 2017 14:21:50 -0700 Subject: [PATCH 02/10] backported CLI options for domain command --- admin/manage.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/admin/manage.py b/admin/manage.py index 095f6f72..f1df913c 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -50,7 +50,10 @@ def user(localpart, domain_name, password, hash_scheme='SHA512-CRYPT'): db.session.add(user) db.session.commit() -@manager.command +@manager.option('-n','--domain_name',dest='domain_name') +@manager.option('-u','--max_users',dest='max_users') +@manager.option('-a','--max_aliases',dest='max_aliases') +@manager.option('-q','--max_quota_bytes',dest='max_quota_bytes') def domain(domain_name, max_users=0, max_aliases=0, max_quota_bytes=0): domain = models.Domain.query.get(domain_name) if not domain: From 319b3e68bb55634bfc4c92565fa712566664e12a Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Wed, 27 Sep 2017 11:05:29 -0700 Subject: [PATCH 03/10] backported proper empty field handling --- admin/manage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/admin/manage.py b/admin/manage.py index f1df913c..e2d4e5ac 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -118,8 +118,8 @@ def config_update(verbose=False, delete_objects=False): print(str(user_config)) localpart=user_config['localpart'] domain_name=user_config['domain'] - password_hash=user_config['password_hash'] - hash_scheme=user_config['hash_scheme'] + password_hash=user_config.get('password_hash',None) + hash_scheme=user_config.get('hash_scheme',None) domain = models.Domain.query.get(domain_name) email='{0}@{1}'.format(localpart,domain_name) if not domain: From 454264ba31b559f0dd28331a0179f77bc16235bc Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Wed, 27 Sep 2017 19:44:19 -0700 Subject: [PATCH 04/10] fix typo --- admin/manage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/manage.py b/admin/manage.py index e2d4e5ac..45c2596e 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -173,7 +173,7 @@ def config_update(verbose=False, delete_objects=False): if verbose: print(str(manager_config)) domain_name = manager_config['domain'] - user_name = manage_config['user'] + user_name = manager_config['user'] domain = models.Domain.query.get(domain_name) manageruser = models.User.query.get(user_name + '@' + domain_name) domain.managers.append(manageruser) From 977fcd01e016377563c843efadbe386ca574934e Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Wed, 27 Sep 2017 20:16:23 -0700 Subject: [PATCH 05/10] add more verbosity for operations --- admin/manage.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/admin/manage.py b/admin/manage.py index 45c2596e..bfa7dbda 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -92,7 +92,6 @@ def config_update(verbose=False, delete_objects=False): tracked_domains=set() for domain_config in domains: if verbose: - print(str(domain_config)) domain_name = domain_config['name'] max_users=domain_config.get('max_users',0) max_aliases=domain_config.get('max_aliases',0) @@ -105,11 +104,13 @@ def config_update(verbose=False, delete_objects=False): max_aliases=max_aliases, max_quota_bytes=max_quota_bytes) db.session.add(domain) + print("Added "+str(domain_config)) else: domain.max_users = max_users domain.max_aliases = max_aliases domain.max_quota_bytes = max_quota_bytes db.session.add(domain) + print("Updated "+str(domain_config)) users=new_config.get('users',[]) tracked_users=set() @@ -182,12 +183,18 @@ def config_update(verbose=False, delete_objects=False): if delete_objects: for user in db.session.query(models.User).all(): if not ( user.email in tracked_users ): + if verbose: + print("Deleting user: "+str(user.email)) db.session.delete(user) for alias in db.session.query(models.Alias).all(): if not ( alias.email in tracked_aliases ): + if verbose: + print("Deleting alias: "+str(alias.email)) db.session.delete(alias) for domain in db.session.query(models.Domain).all(): if not ( domain.name in tracked_domains ): + if verbose: + print("Deleting domain: "+str(domain.name)) db.session.delete(domain) db.session.commit() From 4fed48eae117f9fce9c9722526b53691f93b4cbe Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Wed, 27 Sep 2017 20:31:51 -0700 Subject: [PATCH 06/10] commit earlier to fix issues with missing domains --- admin/manage.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/admin/manage.py b/admin/manage.py index bfa7dbda..8790fc1b 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -168,6 +168,8 @@ def config_update(verbose=False, delete_objects=False): alias.destination = destination db.session.add(alias) + db.session.commit() + managers=new_config.get('managers',[]) # tracked_managers=set() for manager_config in managers: @@ -180,6 +182,8 @@ def config_update(verbose=False, delete_objects=False): domain.managers.append(manageruser) db.session.add(domain) + db.session.commit() + if delete_objects: for user in db.session.query(models.User).all(): if not ( user.email in tracked_users ): From 06d7828e0605dadc48e7578a0f0d1b61c626e282 Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Wed, 27 Sep 2017 20:52:03 -0700 Subject: [PATCH 07/10] fix multi-add of managers --- admin/manage.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/admin/manage.py b/admin/manage.py index 8790fc1b..d3df24a8 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -179,7 +179,8 @@ def config_update(verbose=False, delete_objects=False): user_name = manager_config['user'] domain = models.Domain.query.get(domain_name) manageruser = models.User.query.get(user_name + '@' + domain_name) - domain.managers.append(manageruser) + if not manageruser in domain.managers: + domain.managers.append(manageruser) db.session.add(domain) db.session.commit() From c9f124c9b90e7f551797dd26b019a472795deaff Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Wed, 27 Sep 2017 21:29:42 -0700 Subject: [PATCH 08/10] expand user and alias params --- admin/manage.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/admin/manage.py b/admin/manage.py index d3df24a8..171c7866 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -114,15 +114,24 @@ def config_update(verbose=False, delete_objects=False): users=new_config.get('users',[]) tracked_users=set() + user_optional_params=('comment','quota_bytes','global_admin', + 'enable_imap','enable_pop','forward_enabled','forward_destination', + 'reply_enabled','reply_subject','reply_body','displayed_name','spam_enabled', + 'email','spam_threshold') for user_config in users: if verbose: print(str(user_config)) localpart=user_config['localpart'] domain_name=user_config['domain'] + global_admin=user_config.get('global_admin',False) password_hash=user_config.get('password_hash',None) hash_scheme=user_config.get('hash_scheme',None) domain = models.Domain.query.get(domain_name) email='{0}@{1}'.format(localpart,domain_name) + optional_params={} + for k in user_optional_params: + if user_config.has_key(k): + optional_params[k]=user_config[k] if not domain: domain = models.Domain(name=domain_name) db.session.add(domain) @@ -133,7 +142,7 @@ def config_update(verbose=False, delete_objects=False): user = models.User( localpart=localpart, domain=domain, - global_admin=False + **optional_params ) user.set_password(password_hash, hash_scheme=hash_scheme, raw=True) db.session.add(user) @@ -146,6 +155,11 @@ def config_update(verbose=False, delete_objects=False): localpart=alias_config['localpart'] domain_name=alias_config['domain'] destination=alias_config['destination'] + if type(alias_config['destination']) == str: + destination = alias_config['destination'].split(',') + else: + destination = alias_config['destination'] + wildcard=alias_config.get('wildcard',False) domain = models.Domain.query.get(domain_name) email='{0}@{1}'.format(localpart,domain_name) if not domain: @@ -158,14 +172,13 @@ def config_update(verbose=False, delete_objects=False): alias = models.Alias( localpart=localpart, domain=domain, - # destination=destination.split(','), + wildcard=wildcard, + destination=destination, email=email ) else: - if type(destination) == str: - alias.destination = destination.split(',') - else: - alias.destination = destination + alias.destination = destination + alias.wildcard=wildcard db.session.add(alias) db.session.commit() From a6944582a3139d38079653ca5c666769edf7455d Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Wed, 27 Sep 2017 21:34:10 -0700 Subject: [PATCH 09/10] fix python2-ism --- admin/manage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/manage.py b/admin/manage.py index 171c7866..281a33ed 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -130,7 +130,7 @@ def config_update(verbose=False, delete_objects=False): email='{0}@{1}'.format(localpart,domain_name) optional_params={} for k in user_optional_params: - if user_config.has_key(k): + if k in user_config: optional_params[k]=user_config[k] if not domain: domain = models.Domain(name=domain_name) From 83e6d4400cf805ad0f9758748b38f708d1d33b93 Mon Sep 17 00:00:00 2001 From: Dmytro Makovey Date: Wed, 27 Sep 2017 21:48:20 -0700 Subject: [PATCH 10/10] fix user params updates --- admin/manage.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/admin/manage.py b/admin/manage.py index 281a33ed..9ed9e692 100644 --- a/admin/manage.py +++ b/admin/manage.py @@ -144,6 +144,9 @@ def config_update(verbose=False, delete_objects=False): domain=domain, **optional_params ) + else: + for k in optional_params: + setattr(user, k, optional_params[k]) user.set_password(password_hash, hash_scheme=hash_scheme, raw=True) db.session.add(user)