Parcourir la source

Fix #64 - user add workin'

George S. Baugh il y a 5 ans
Parent
commit
002815ecc0
4 fichiers modifiés avec 19 ajouts et 11 suppressions
  1. 2 2
      lib/Trog/Auth.pm
  2. 1 2
      lib/Trog/Data/DUMMY.pm
  3. 14 5
      lib/Trog/Routes/HTML.pm
  4. 2 2
      www/templates/profile.tx

+ 2 - 2
lib/Trog/Auth.pm

@@ -88,12 +88,12 @@ sub useradd ($user, $pass, $acls) {
     my $dbh = _dbh();
     my $salt = create_uuid();
     my $hash = sha256($pass.$salt);
-    my $res =  $dbh->do("INSERT INTO user (name,salt,hash) VALUES (?,?,?)", undef, $user, $salt, $hash);
+    my $res =  $dbh->do("INSERT OR REPLACE INTO user (name,salt,hash) VALUES (?,?,?)", undef, $user, $salt, $hash);
     return unless $res && ref $acls eq 'ARRAY';
 
     #XXX this is clearly not normalized with an ACL mapping table, will be an issue with large number of users
     foreach my $acl (@$acls) {
-        return unless $dbh->do("INSERT INTO user_acl (user_id,acl) VALUES ((SELECT id FROM user WHERE name=?),?)", undef, $user, $acl);
+        return unless $dbh->do("INSERT OR REPLACE INTO user_acl (user_id,acl) VALUES ((SELECT id FROM user WHERE name=?),?)", undef, $user, $acl);
     }
     return 1;
 }

+ 1 - 2
lib/Trog/Data/DUMMY.pm

@@ -170,7 +170,7 @@ sub _add_post_type (@posts) {
         my $type = 'file';
         $type = 'blog'      if grep { $_ eq 'blog' }    @{$post->{tags}};
         $type = 'microblog' if grep { $_ eq 'news' }    @{$post->{tags}};
-        $type = 'profile'   if grep { $_ eq 'profile' } @{$post->{tags}};
+        $type = 'profile'   if grep { $_ eq 'about' } @{$post->{tags}};
         $type = 'series'    if grep { $_ eq 'series'  } @{$post->{tags}};
         $post->{type} = $type;
         $post
@@ -209,7 +209,6 @@ sub add ($self, @posts) {
             $post->{version}++;
         }
         $post->{version} //= 0;
-        #TODO set local_href and content type correctly
 
         $post = _process($post);
         push @$example_posts, $post;

+ 14 - 5
lib/Trog/Routes/HTML.pm

@@ -401,11 +401,6 @@ sub config_save ($query, $render_cb) {
     return config($query, $render_cb);
 }
 
-# TODO actually do stuff
-sub profile ($query, $render_cb) {
-    return config($query, $render_cb);
-}
-
 =head2 themeclone
 
 Clone a theme by copying a directory.
@@ -497,6 +492,20 @@ sub post_save ($query, $render_cb) {
     return post($query, $render_cb);
 }
 
+sub profile ($query, $render_cb) {
+    #TODO allow users to do something OTHER than be admins
+    if ($query->{password}) {
+        Trog::Auth::useradd($query->{username}, $query->{password}, ['admin'] );
+    }
+
+    #Make sure it is "self-authored", redact pw
+    $query->{user} = delete $query->{username};
+    delete $query->{password};
+
+    return post_save($query, $render_cb);
+}
+
+
 sub post_delete ($query, $render_cb) {
     state $data = Trog::Data->new($conf);
     $query->{failure} = $data->delete($query);

+ 2 - 2
www/templates/profile.tx

@@ -1,6 +1,6 @@
 <form class="Submissions" action="/profile" method="POST" enctype="multipart/form-data">
-    Username *<br /><input required class="cooltext" type="text" name="title" placeholder="AzureDiamond" value="<: $post.user :>" />
-    Password *<br /><input required class="cooltext" type="password" name="password" placeholder="hunter2" />
+    Username *<br /><input required class="cooltext" type="text" name="username" placeholder="AzureDiamond" value="<: $post.user :>" />
+    Password *<br /><input <: $post.user ? '' : 'required' :> class="cooltext" type="password" name="password" placeholder="hunter2" />
     Avatar *<br /><input class="cooltext" type="file" name="preview_file" />
     : if ( $post.preview ) {
     <input type="hidden" name="preview" value="<: $post.preview :>" />