Admin can select groups in user creation
authorGuillaume Subiron <maethor@subiron.org>
Wed, 30 May 2012 07:37:44 +0000 (09:37 +0200)
committerJulien Rabier <taziden@flexiden.org>
Wed, 30 May 2012 07:50:35 +0000 (09:50 +0200)
main.py
schema.sql
templates/admin_user_new.html
templates/admin_users.html

diff --git a/main.py b/main.py
index b00b879..2ab60f8 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -148,7 +148,8 @@ def user_password(userid):
 def admin_users():
     if not session.get('user').get('is_admin'):
         abort(401)
-    users = query_db('select * from users order by id desc')
+    users = query_db('select *, roles.name as rolename from (select *, name as username from users join user_role on id=id_user order by id desc) join roles on id_role=roles.id')
+    # :TODO:maethor:20120530: Find a way to reduce the dict
     return render_template('admin_users.html', users=users)
 
 @app.route('/admin/users/add', methods=['GET', 'POST'])
@@ -165,12 +166,23 @@ def admin_user_add():
             g.db.execute('insert into users (email, name, organization, password, is_admin) values (?, ?, ?, ?, ?)',
                     [request.form['email'], request.form['username'], request.form['organization'], password, admin])
             g.db.commit()
-            # :TODO:maethor:120528: Send mail
-            flash(u'Le nouvel utilisateur a été créé avec succès', 'success')
-            return redirect(url_for('home'))
+            user = query_db('select * from users where email = ?', [request.form["email"]], one=True)
+            if user:
+              for role in request.form.getlist('roles'):
+                  # :TODO:maethor:120528: Check if this role exist
+                  if query_db('select id from roles where id = ?', role, one=True) is None:
+                      abort(401)
+                  g.db.execute('insert into user_role values (?, ?)', [user['id'], role])
+                  g.db.commit()
+              # :TODO:maethor:120528: Send mail
+              flash(u'Le nouvel utilisateur a été créé avec succès', 'success')
+              return redirect(url_for('admin_users'))
+            else:
+                flash(u'Une erreur s\'est produite.', 'error')
         else:
             flash(u"Vous devez spécifier une adresse email.", 'error')
-    return render_template('admin_user_new.html')
+    groups = query_db('select * from roles where system=0') 
+    return render_template('admin_user_new.html', groups=groups)
 
 #-------------
 # Roles admin
index b24a7f3..c1805c4 100644 (file)
@@ -20,6 +20,14 @@ create table roles (
     system INTEGER default 0 not null 
 );
 
+create table user_role (
+    id_user INTEGER,
+    id_role INTEGER,
+    FOREIGN KEY(id_user) REFERENCES users(id),
+    FOREIGN KEY(id_role) REFERENCES roles(id),
+    PRIMARY KEY(id_user, id_role)
+);
+
 create table votes (
     id INTEGER primary key autoincrement,
     title TEXT not null,
index e3678d0..82c615f 100644 (file)
     </div>
   </div>
   <div class="control-group">
-    <label class="control-label" for="role">Groupes</label>
+    <label class="control-label" for="roles">Groupes</label>
     <div class="controls">
-      <select name="role" id="role" multiple>
-        <option selected>CA</option>
-        <option>Membres</option>
+      <select name="roles" id="roles" multiple>
+        {% for group in groups %}
+        <option value="{{ group.id }}">{{ group.name }}</option>
+        {% endfor %}
       </select>
     </div>
   </div>
index 937f885..8ec9eb3 100644 (file)
@@ -20,8 +20,8 @@
     {% for user in users %}
     <tr>
       <td>{{ user.email }}</td>
-      <td>{{ user.name }}</td>
-      <td></td>
+      <td>{{ user.username }}</td>
+      <td>{{ user.rolename }}</td>
       <td>{% if user.is_admin %}<span class="label label-success">Oui</span>{% else %}<span class="label">Non</span>{% endif %}</td>
       <td>
         <a href="" class="btn btn-mini">Éditer</a>