tenant view only shows sites the user should be able to see
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-backbone.js
index 28fd72b..1ca1307 100644 (file)
@@ -20,7 +20,10 @@ if (! window.XOSLIB_LOADED ) {
     NETWORKDEPLOYMENT_API = "/plstackapi/networkdeployments/";
     FLAVOR_API = "/plstackapi/flavors/";
     CONTROLLER_API = "/plstackapi/controllers/";
+
+    /* removed
     CONTROLLERSITEDEPLOYMENT_API = "/plstackapi/controllersitedeploymentses";
+    */
 
     /* changed as a side effect of the big rename
     SLICEDEPLOYMENT_API = "/plstackapi/slice_deployments/";
@@ -31,8 +34,16 @@ if (! window.XOSLIB_LOADED ) {
     USERDEPLOYMENT_API = "/plstackapi/userdeployments/";
 
     SLICEPLUS_API = "/xoslib/slicesplus/";
+    TENANTVIEW_API = "/xoslib/tenantview/"
 
     XOSModel = Backbone.Model.extend({
+        relatedCollections: [],
+        foreignCollections: [],
+        foreignFields: {},
+        m2mFields: {},
+        readonlyFields: [],
+        detailLinkFields: [],
+
         /* from backbone-tastypie.js */
         //idAttribute: 'resource_uri',
 
@@ -149,6 +160,10 @@ if (! window.XOSLIB_LOADED ) {
 \r
         relatedCollections: [],\r
         foreignCollections: [],\r
+        foreignFields: {},\r
+        m2mFields: {},
+        readonlyFields: [],
+        detailLinkFields: [],\r
 \r
         sorted: function() {\r
             //console.log("sorted " + this.modelName);\r
@@ -304,18 +319,12 @@ if (! window.XOSLIB_LOADED ) {
                     var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
                     url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
 
-                    // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
-                    // (set to 'resource_uri') contains the model's id.
-                    if ( models && models.length ) {
-                            var ids = _.map( models, function( model ) {
-                                            var parts = _.compact( model.id.split('/') );
-                                            return parts[ parts.length - 1 ];
-                                    });
-                            url += 'set/' + ids.join(';') + '/';
-                    }
-
                     url && ( url += "?no_hyperlinks=1" );
 
+                    if (this.currentUserCanSee) {
+                        url && ( url += "&current_user_can_see=1" );
+                    }
+
                     return url;
             },
 
@@ -330,9 +339,26 @@ if (! window.XOSLIB_LOADED ) {
             },
     });
 
+    function get_defaults(modelName) {
+        if ((typeof xosdefaults !== "undefined") && xosdefaults[modelName]) {
+            return xosdefaults[modelName];
+        }
+        return undefined;
+    }
+
+    function extend_defaults(modelName, stuff) {
+        defaults = get_defaults(modelName);
+        if (defaults) {
+            return $.extend({}, defaults, stuff);
+        } else {
+            return stuff;
+        }
+    }
+
     function define_model(lib, attrs) {
         modelName = attrs.modelName;
         modelClassName = modelName;
+        collectionClass = attrs.collectionClass || XOSCollection;
         collectionClassName = modelName + "Collection";
 
         if (!attrs.addFields) {
@@ -355,7 +381,7 @@ if (! window.XOSLIB_LOADED ) {
 
         for (key in attrs) {
             value = attrs[key];
-            if ($.inArray(key, ["urlRoot", "modelName", "collectionName", "listFields", "addFields", "detailFields", "detailLinkFields", "foreignFields", "inputType", "relatedCollections", "foreignCollections"])>=0) {
+            if ($.inArray(key, ["urlRoot", "modelName", "collectionName", "listFields", "addFields", "detailFields", "detailLinkFields", "foreignFields", "inputType", "relatedCollections", "foreignCollections", "defaults"])>=0) {
                 modelAttrs[key] = value;
                 collectionAttrs[key] = value;
             }
@@ -364,10 +390,14 @@ if (! window.XOSLIB_LOADED ) {
             }
         }
 
-        if ((typeof xosdefaults !== "undefined") && xosdefaults[modelName]) {
-            modelAttrs["defaults"] = xosdefaults[modelName];
+        if (!modelAttrs.defaults) {
+            modelAttrs.defaults = get_defaults(modelName);
         }
 
+//        if ((typeof xosdefaults !== "undefined") && xosdefaults[modelName]) {
+//            modelAttrs["defaults"] = xosdefaults[modelName];
+//        }
+
         if ((typeof xosvalidators !== "undefined") && xosvalidators[modelName]) {
             modelAttrs["validators"] = xosvalidators[modelName];
         }
@@ -376,7 +406,7 @@ if (! window.XOSLIB_LOADED ) {
 
         collectionAttrs["model"] = lib[modelName];
 
-        lib[collectionClassName] = XOSCollection.extend(collectionAttrs);
+        lib[collectionClassName] = collectionClass.extend(collectionAttrs);
         lib[collectionName] = new lib[collectionClassName]();
 
         lib.allCollectionNames.push(collectionName);
@@ -500,7 +530,6 @@ if (! window.XOSLIB_LOADED ) {
                             });
 
         define_model(this, {urlRoot: SITEDEPLOYMENT_API,
-                            relatedCollections: {"controllerSiteDeployment": "site_deployment"},
                             foreignCollections: ["sites", "deployments", "controllers"],
                             foreignFields: {"site": "sites", "deployment": "deployments", "controller": "controllers"},
                             modelName: "siteDeployment",
@@ -576,6 +605,7 @@ if (! window.XOSLIB_LOADED ) {
                             detailFields: ["backend_status", "name", "version", "backend_type", "auth_url", "admin_user", "admin_password", "admin_tenant"],
                             });
 
+        /* removed
         define_model(this, {urlRoot: CONTROLLERSITEDEPLOYMENT_API,
                             modelName: "controllerSiteDeployment",
                             foreignCollections: ["site_deployments", "controllers"],
@@ -583,6 +613,7 @@ if (! window.XOSLIB_LOADED ) {
                             listFields: ["backend_status", "id", "site_deployment", "controller", "tenant_id"],
                             detailFields: ["backend_status", "site_deployment", "controller", "tenant_id"],
                             });
+        */
 
         /* DELETED in site-controller branch
 
@@ -625,9 +656,41 @@ if (! window.XOSLIB_LOADED ) {
         // enhanced REST
         // XXX this really needs to somehow be combined with Slice, to avoid duplication
         define_model(this, {urlRoot: SLICEPLUS_API,
-                            relatedCollections: {'slivers': "slice"},
-                            modelName: "slicePlus",
-                            collectionName: "slicesPlus"});
+                           relatedCollections: {"slivers": "slice", "slicePrivileges": "slice", "networks": "owner"},
+                           foreignCollections: ["services", "sites"],
+                           foreignFields: {"service": "services", "site": "sites"},
+                           listFields: ["backend_status", "id", "name", "enabled", "description", "slice_url", "site", "max_slivers", "service"],
+                           detailFields: ["backend_status", "name", "site", "enabled", "description", "slice_url", "max_slivers"],
+                           inputType: {"enabled": "checkbox"},
+                           modelName: "slicePlus",
+                           collectionName: "slicesPlus",
+                           defaults: extend_defaults("slice", {"network_ports": "", "site_allocation": []}),
+                           xosValidate: function(attrs, options) {
+                               errors = XOSModel.prototype.xosValidate(this, attrs, options);
+                               // validate that slice.name starts with site.login_base
+                               site = attrs.site || this.site;
+                               if ((site!=undefined) && (attrs.name!=undefined)) {
+                                   site = xos.sites.get(site);
+                                   if (attrs.name.indexOf(site.attributes.login_base+"_") != 0) {
+                                        errors = errors || {};
+                                        errors["name"] = "must start with " + site.attributes.login_base + "_";
+                                   }
+                               }
+                               return errors;
+                             },
+                           });
+
+        define_model(this, {urlRoot: TENANTVIEW_API,
+                            modelName: "tenantview",
+                            collectionName: "tenantview",
+                            listFields: [],
+                            detailFields: [],
+                            });
+
+        /* by default, have slicePlus only fetch the slices the user can see */
+        this.slicesPlus.currentUserCanSee = true;
+
+        this.tenant = function() { return this.tenantview.models[0].attributes; };
 
         this.listObjects = function() { return this.allCollectionNames; };