Loop protect for org unit editor
authorThomas Berezansky <tsbere@mvlc.org>
Thu, 16 Jun 2011 02:03:26 +0000 (22:03 -0400)
committerThomas Berezansky <tsbere@mvlc.org>
Thu, 16 Jun 2011 02:03:26 +0000 (22:03 -0400)
Don't write to backend store and mark field as invalid when a
parent_ou loop is detected during edit.

Note: Multiple edits that may introduce a loop are not guaranteed
to save properly unless saved one at a time as they are made.

Signed-off-by: Thomas Berezansky <tsbere@mvlc.org>
Open-ILS/web/conify/global/actor/org_unit.html

index 726898b..ff7f506 100644 (file)
                                                                        >
                                                                                <script type="dojo/method" event="onChange">
 <![CDATA[
-                                                                                       if (current_ou && this.getValue()) this.store.setValue( current_ou, "parent_ou", this.getValue() );
-                                                                                       if ( this.store.getValue( current_ou, '_trueRoot' ) == 'true' ) this.required = false;
+                                                                                       if (current_ou && this.getValue()) {
+                                                if(this.store.getValue( current_ou, 'id' ) == this.getValue()) {
+                                                    this.isValid = function() { return false; };
+                                                    return;
+                                                }
+                                                var seen_ous = {};
+                                                seen_ous[this.store.getValue( current_ou, 'id' )] = true;
+                                                var temp_ou;
+                                                ou_list_store.fetch({
+                                                    query : { id : this.getValue() },
+                                                    queryOptions : { deep : true },
+                                                    onItem : function (item, req) { if (this.isItem( item )) temp_ou = item; },
+                                                    scope : ou_list_store
+                                                });
+                                                for(var i = 0; i < 100; i++) {
+                                                    if(!this.store.getValue( temp_ou, 'parent_ou' ) ) {
+                                                        break;
+                                                    }
+                                                    if(seen_ous[this.store.getValue( temp_ou, 'parent_ou' )]) {
+                                                        this.isValid = function() { return false; };
+                                                        return;
+                                                    }
+                                                    ou_list_store.fetch({
+                                                        query : { id : this.store.getValue( temp_ou, 'parent_ou') },
+                                                        queryOptions : { deep : true },
+                                                        onItem : function (item, req) { if (this.isItem( item )) temp_ou = item; },
+                                                        scope : ou_list_store
+                                                    });
+                                                }
+                                                if(i == 100) {
+                                                    this.isValid = function() { return false; };
+                                                    return;
+                                                }
+                                                this.isValid = function() { return true; };
+                                                this.store.setValue( current_ou, "parent_ou", this.getValue() );
+                                            }
+                                                                                       if ( this.store.getValue( current_ou, '_trueRoot' ) == 'true' ) {
+                                                this.required = false;
+                                                this.isValid = function() { return true; };
+                                            }
                                                                                        else this.required = true;
 ]]>
                                                                                </script>