<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>com_rohea_account/templates/cra-show-remove.xhtml</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -29,6 +29,15 @@ routes:
         action: admin
         route: /admin/
         content_entry_point: cra-show-admin
+    admin_remove:
+        controller: com_rohea_account_controllers_admin
+        action: remove
+        route: '/admin/{$username}'
+        content_entry_point: cra-show-remove
+        allowed_methods:
+            - OPTIONS
+            - GET
+            - POST
     settings:
         controller: com_rohea_account_controllers_settings
         action: settings</diff>
      <filename>com_rohea_account/configuration/defaults.yml</filename>
    </modified>
    <modified>
      <diff>@@ -19,11 +19,10 @@ class com_rohea_account_controllers_admin
     {
         $this-&gt;configuration = $instance-&gt;configuration;
     }
-    
 
     public function action_admin($route_id, &amp;$data, $args)
     {
-        //TODO: Admin ACL check !!!
+        $_MIDCOM-&gt;authorization-&gt;require_admin();
         
         $data['name'] = &quot;com_rohea_account&quot;;
         
@@ -32,7 +31,8 @@ class com_rohea_account_controllers_admin
         $registered_persons = array();
         foreach($persons as $p)
         {
-            if($p-&gt;username != $p-&gt;guid)
+            $p-&gt;remove_url = $_MIDCOM-&gt;dispatcher-&gt;generate_url('admin_remove', array('username' =&gt; $p-&gt;username), $_MIDCOM-&gt;context-&gt;page);
+            if ($p-&gt;username != $p-&gt;guid)
             {
                 $registered_persons[] = $p;
             }        
@@ -40,6 +40,193 @@ class com_rohea_account_controllers_admin
         $data['registered_persons'] = $registered_persons;
     }
     
+    private function get_linked_classes()
+    {
+        static $linked = array();
+        if (!empty($linked))
+        {
+            // Already built
+            return $linked;
+        }
+
+        foreach ($_MIDGARD['schema']['types'] as $classname =&gt; $null)
+        {
+            $reflector = new midgard_reflection_property($classname);
+            $dummy = new $classname();
+            $properties = get_object_vars($dummy);
+            foreach ($properties as $property =&gt; $default)
+            {
+                if (!$reflector-&gt;is_link($property))
+                {
+                    continue;
+                }
+                
+                $link_type = $reflector-&gt;get_midgard_type($property);
+                if ($link_type == MGD_TYPE_UINT)
+                {
+                    $target_property = 'id';
+                }
+                elseif ($link_type == MGD_TYPE_GUID)
+                {
+                    $target_property = 'guid';
+                }
+                else
+                {
+                    // Don't deal with the other types of links for now
+                    continue;
+                }
+                
+                $target_class = $reflector-&gt;get_link_name($property);
+    
+                if (!isset($linked[$target_class]))
+                {
+                    $linked[$target_class] = array();
+                }
+                if (!isset($linked[$target_class][$target_property]))
+                {
+                    $linked[$target_class][$target_property] = array();
+                }
+
+                $linked[$target_class][$target_property][] = array
+                (
+                    'class' =&gt; $classname,
+                    'property' =&gt; $property,
+                );
+            }
+        }
+        return $linked;
+    }
+    
+    private function get_dependencies_for($object, $count_only = false)
+    {
+        $classname = get_class($object);
+        $linked = $this-&gt;get_linked_classes();
+        if (!isset($linked[$classname]))
+        {
+            // Nothing links to this class
+            return array();
+        }
+        
+        $dependencies = array();
+        
+        foreach ($linked[$classname] as $property =&gt; $links)
+        {
+            if (!$object-&gt;$property)
+            {
+                continue;
+            }
+            
+            foreach ($links as $link)
+            {
+                $qb = new midgard_query_builder($link['class']);
+                $qb-&gt;add_constraint($link['property'], '=', $object-&gt;$property);
+                
+                if ($count_only)
+                {
+                    $deps = $qb-&gt;count();
+                    if ($deps &gt; 0)
+                    {
+                        $dependencies[] = array
+                        (
+                            'class' =&gt; $link['class'],
+                            'property' =&gt; $link['property'],
+                            'dependencies' =&gt; $deps,
+                            'actionname' =&gt; &quot;action[{$link['class']}]&quot;,
+                        );
+                    }
+                    continue;
+                }
+                
+                $deps = $qb-&gt;execute();
+                if (count($deps) &gt; 0)
+                {
+                    $dependencies[] = array
+                    (
+                        'class' =&gt; $link['class'],
+                        'property' =&gt; $link['property'],
+                        'dependencies' =&gt; $deps,
+                        'actionname' =&gt; &quot;action[{$link['class']}]&quot;,
+                    );
+                }
+            }
+        }
+        return $dependencies;
+    }
+    
+    private function get_user_by_username($username)
+    {
+        $qb = new midgard_query_builder('midgard_person');
+        $qb-&gt;add_constraint('username', '=', $username);
+        $users = $qb-&gt;execute();
+        if (count($users) == 0)
+        {
+            throw new midcom_exception_notfound(&quot;User {$username} not found.&quot;);
+        }
+        return $users[0];
+    }
     
+    public function action_remove($route_id, &amp;$data, $args)
+    {
+        $_MIDCOM-&gt;authorization-&gt;require_admin();
+        $data['person'] = $this-&gt;get_user_by_username($args['username']);
+        
+        if (   isset($_POST['action'])
+            &amp;&amp; is_array($_POST['action']))
+        {
+            $assign_to = $this-&gt;get_user_by_username($_POST['assign_to_username']);
+            $dependencies = $this-&gt;get_dependencies_for($data['person']);
+            $linked = $this-&gt;get_linked_classes();
+            foreach ($dependencies as $dependency)
+            {
+                if (!isset($_POST['action'][$dependency['class']]))
+                {
+                    // No action defined for these deps, skip
+                    continue;
+                }
+                $class_action = $_POST['action'][$dependency['class']];
+
+                foreach ($dependency['dependencies'] as $dep)
+                {
+                    switch ($class_action)
+                    {
+                        case 'delete':
+                            // TODO: Subdeps
+                            $dep-&gt;delete();
+                            break;
+                        case 'assign':
+                            foreach ($linked[get_class($data['person'])] as $property =&gt; $links)
+                            {
+                                foreach ($links as $link)
+                                {
+                                    if ($link['class'] != $dependency['class'])
+                                    {
+                                        continue;
+                                    }
+                                    
+                                    $linked_property = $link['property'];
+                                    $dep-&gt;$linked_property = $assign_to-&gt;$property;
+                                    $dep-&gt;update();
+                                }
+                            }
+                            break;
+                    }
+                }
+            }
+
+            // Check that all dependencies are handled
+            $data['dependency_counts'] = $this-&gt;get_dependencies_for($data['person'], true);
+            if (!empty($data['dependency_counts']))
+            {
+                // Go back to view, some objects remain unhandled
+                return;
+            }
+            
+            $data['person']-&gt;delete();
+            header('Location: ' . $_MIDCOM-&gt;dispatcher-&gt;generate_url('admin', array(), $_MIDCOM-&gt;context-&gt;page));
+            die();
+        }
+        
+        $data['dependency_counts'] = $this-&gt;get_dependencies_for($data['person'], true);
+    }
 }
 ?&gt;</diff>
      <filename>com_rohea_account/controllers/admin.php</filename>
    </modified>
    <modified>
      <diff>@@ -16,7 +16,7 @@ margin: 0;
     &lt;td&gt;&lt;p&gt;&lt;strong&gt;del&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr tal:repeat=&quot;person com_rohea_account/registered_persons&quot;&gt;
-    &lt;td&gt;&lt;p&gt;${person/id}&lt;/p&gt;&lt;/td&gt;
+    &lt;td&gt;&lt;p&gt;&lt;a href=&quot;#&quot; tal:attributes=&quot;href person/remove_url&quot;&gt;${person/id}&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;
     &lt;td&gt;&lt;p&gt;${person/username}&lt;/p&gt;&lt;/td&gt;
     &lt;td&gt;&lt;p&gt;${person/guid}&lt;/p&gt;&lt;/td&gt;
     &lt;td&gt;&lt;p&gt;${person/metadata/deleted}&lt;/p&gt;&lt;/td&gt;</diff>
      <filename>com_rohea_account/templates/cra-show-admin.xhtml</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>f9c0eeebdd0ec7cceb035fe893f1c5781f6ccef6</id>
    </parent>
  </parents>
  <author>
    <name>Henri Bergius</name>
    <email>henri.bergius@iki.fi</email>
  </author>
  <url>http://github.com/bergie/midcom/commit/1022fbb54c6b5d59cccfed9303bf917cb41b4c8f</url>
  <id>1022fbb54c6b5d59cccfed9303bf917cb41b4c8f</id>
  <committed-date>2009-04-09T04:46:07-07:00</committed-date>
  <authored-date>2009-04-09T04:46:07-07:00</authored-date>
  <message>Tool for user removal add dependency reassignment


git-svn-id: https://svn.midgard-project.org/midgard/trunk/midcom@21754 62f49d1c-1c16-0410-9156-d311ce72abac</message>
  <tree>bc68cd8ce583635b728f0faf4a4349ae7c9adfdb</tree>
  <committer>
    <name>Henri Bergius</name>
    <email>henri.bergius@iki.fi</email>
  </committer>
</commit>
