Fix requirements on groups and users

Change-Id: I9767e07ffe3a808ee9a2de925862ba193b7b612e
diff --git a/linux/system/group.sls b/linux/system/group.sls
index 2c1c769..d990889 100644
--- a/linux/system/group.sls
+++ b/linux/system/group.sls
@@ -7,7 +7,7 @@
 
 system_group_{{ group_name }}:
   group.present:
-  - name: {{ group.name }}
+  - name: {{ group.get('name', group_name) }}
   {%- if group.system is defined and group.system %}
   - system: True
   {%- endif %}
diff --git a/linux/system/job.sls b/linux/system/job.sls
index c0c6b5f..f0373a4 100644
--- a/linux/system/job.sls
+++ b/linux/system/job.sls
@@ -23,6 +23,10 @@
     {%- if job.dayweek is defined %}
     - dayweek: '{{ job.dayweek }}'
     {%- endif %}
+    {%- if job.user in system.get('user', {}).keys() %}
+    - require:
+      - user: system_user_{{ job.user }}
+    {%- endif %}
   {%- else %}
   cron.absent:
     - name: {{ job.command }}
diff --git a/linux/system/user.sls b/linux/system/user.sls
index 70c9c51..5c6ba92 100644
--- a/linux/system/user.sls
+++ b/linux/system/user.sls
@@ -5,6 +5,14 @@
 
 {%- if user.enabled %}
 
+{%- set requires = [] %}
+{%- for group in user.get('groups', []) %}
+  {%- if group in system.get('group', {}).keys() %}
+    {%- do requires.append({'group': 'system_group_'+group}) %}
+  {%- endif %}
+{%- endfor %}
+
+
 system_user_{{ name }}:
   user.present:
   - name: {{ name }}
@@ -25,6 +33,7 @@
   {%- if user.uid is defined and user.uid %}
   - uid: {{ user.uid }}
   {%- endif %}
+  - require: {{ requires|yaml }}
 
 system_user_home_{{ user.home }}:
   file.directory:
diff --git a/tests/pillar/system.sls b/tests/pillar/system.sls
index 8d34cc6..58358f8 100644
--- a/tests/pillar/system.sls
+++ b/tests/pillar/system.sls
@@ -43,7 +43,8 @@
         full_name: Test User
         home: /home/test
         groups:
-          - root
+          - db-ops
+          - salt-ops
       salt_user1:
         enabled: true
         name: saltuser1
@@ -58,18 +59,19 @@
         uid: 9992
         full_name: Salt Sudo User2
         home: /home/saltuser2
+        groups:
+          - sudogroup1
     group:
-      testgroup:
+      test:
         enabled: true
         name: test
         gid: 9999
         system: true
       db-ops:
         enabled: true
-        name: testgroup
       salt-ops:
         enabled: true
-        name: sudogroup0
+        name: salt-ops
       sudogroup1:
         enabled: true
         name: sudogroup1