# $Id: s.UserMgt.xotcl 1.10 01/03/23 21:55:31+01:00 neumann@somewhere.wu-wien.ac.at $
package provide xotcl::awo::userMgt 0.8

Class UserMgt 
Class UserMgt::User -parameter {name password}

UserMgt instproc addUser {name password} {
  [self class]::User [self]::$name -name $name -password $password
}

UserMgt set exportedInstprocs [list \
  addUser \
  listUsers \
  deleteUser \
  userMgtOptions\
]

UserMgt instproc init args {
  next
  [self] exportedProcs [concat [[self] exportedProcs] [[self class] set exportedInstprocs]]
}

UserMgt instproc listUsers {} {
  #showCall
  set users ""
  foreach u [[self] info children] {
    lappend users [namespace tail $u]
  }
  return $users
}

UserMgt instproc deleteUser {name} {
  if {[[self class]::User info instances [self]::$name] != ""} {
    [self]::$name destroy
  }
}
UserMgt instproc userMgtOptions {} {
  return [[self class] set exportedInstprocs]
}

Class UserMgtHtml -superclass HtmlRep

UserMgtHtml instproc addUser args {
  set place [HtmlPlace getInstance]
  if {$args == ""} {
    set action [url encodeItem "[[self] htmlCall] [[self] repObj] [self proc]"]
    set c {
      <form method=get action=$action>
      <p> Name: 
      <input name="name" type=text size=30>
      <p> Password:
      <input name="password" type=password typesize=30>
      <p><p>
      <input type=submit value="Submit">
      <input type=reset value="Reset">
    }
    set c [subst -nobackslashes -nocommands $c]
    
    return [[self] simplePage $place "New User" $c]
  } else {
    if {[llength $args] > 1} {
      set name [lindex $args 0]
      set password [lindex $args 1]
      set user [[[self] repObj] [self proc] $name $password]		
      set c "\n$name entered $place successfully\n"
      return [[self] simplePage "New User" "New User" $c]
    } else {
      #
      # error !!!
    }
    return [[self] [self proc]]
  }
}

UserMgtHtml instproc listUsers {} {
  set c ""
  foreach u [[[self] repObj] [self proc]] {
    append c "<p> $u \n"
  }
  return [[self] simplePage "User List" "User List" $c]  
}

UserMgtHtml instproc userMgtOptions {} {
  set c ""
  foreach u [[[self] repObj] [self proc]] {
    append c "<p> <a href=[[self] selfAction $u]> $u </a>\n"
  }
  return [[self] simplePage "User Management Options" "User Management Options" $c]  
}