Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / models / SunstoneViews.rb @ b9b272f0

History | View | Annotate | Download (5.2 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
require 'yaml'
18
require 'json'
19

    
20
require 'pp'
21

    
22

    
23
# This class is used by Sunstone to set and return the views available to a user
24
# as well as available tabs.
25
class SunstoneViews
26

    
27
    ############################################################################
28
    # Class Constants:
29
    #   - Configuration files
30
    #   - sunstone-views.yaml includes default group views
31
    ############################################################################
32
    VIEWS_CONFIGURATION_FILE = ETC_LOCATION + "/sunstone-views.yaml"
33
    VIEWS_CONFIGURATION_DIR  = ETC_LOCATION + "/sunstone-views/"
34

    
35
    def initialize
36
        @views_config = YAML.load_file(VIEWS_CONFIGURATION_FILE)
37

    
38
        base_path = SUNSTONE_ROOT_DIR+'/public/js/'
39

    
40
        @views = Hash.new
41

    
42
        Dir[VIEWS_CONFIGURATION_DIR+'*.yaml'].each do |p_path|
43
            m = p_path.match(/^#{VIEWS_CONFIGURATION_DIR}(.*).yaml$/)
44
            if m && m[1]
45
                @views[m[1]] = YAML.load_file(p_path)
46
            end
47
        end
48
    end
49

    
50
    def view(user_name, group_name, view_name=nil)
51
        available_views = available_views(user_name, group_name)
52

    
53
        if view_name && available_views.include?(view_name)
54
            return @views[view_name]
55
        else
56
            return @views[available_views.first]
57
        end
58
    end
59

    
60
    # Return the name of the views avialable to a user. Those defined in the
61
    # group template and configured in sunstone. If no view is defined in a
62
    # group defaults in sunstone-views.yaml will be used.
63
    #
64
    def available_views(user_name, group_name)
65
        onec = $cloud_auth.client(user_name)
66
        user = OpenNebula::User.new_with_id(OpenNebula::User::SELF, onec)
67

    
68
        available = Array.new
69

    
70
        rc = user.info
71
        if OpenNebula.is_error?(rc)
72
            return available
73
        end
74

    
75
        user.groups.each { |gid|
76
            group = OpenNebula::Group.new_with_id(gid, onec)
77

    
78
            rc = group.info
79
            if OpenNebula.is_error?(rc)
80
                return available.uniq
81
            end
82

    
83
            if group["TEMPLATE/SUNSTONE/VIEWS"]
84
                views_array = group["TEMPLATE/SUNSTONE/VIEWS"].split(",")
85
                available << views_array.each{|v| v.strip!}
86
            elsif @views_config['groups']
87
                available << @views_config['groups'][group.name]
88
            end
89

    
90
            gadmins       = group.admin_ids
91
            gadmins_views = group["TEMPLATE/SUNSTONE/GROUP_ADMIN_VIEWS"]
92

    
93
            if gadmins && gadmins.include?(user.id) && gadmins_views
94
                views_array = gadmins_views.split(",")
95
                available << views_array.each{|v| v.strip!}
96
            end
97
        }
98

    
99
        available.flatten!
100

    
101
        available.reject!{|v| !@views.has_key?(v)} #sanitize array views
102

    
103
        return available.uniq if !available.empty?
104

    
105
        # Fallback to default views if none is defined in templates
106

    
107
        available << @views_config['users'][user_name] if @views_config['users']
108
        if @views_config['groups']
109
            available << @views_config['groups'][group_name]
110
        end
111
        available << @views_config['default']
112

    
113
        group = OpenNebula::Group.new_with_id(user.gid, onec)
114

    
115
        rc = group.info
116
        if !OpenNebula.is_error?(rc)
117
            gadmins = group.admin_ids
118
            if gadmins && gadmins.include?(user.id)
119
                available << @views_config['default_groupadmin']
120
            end
121
        end
122

    
123
        available.flatten!
124

    
125
        available.reject!{|v| !@views.has_key?(v)} #sanitize array views
126

    
127
        return available.uniq
128
    end
129

    
130
    def get_all_views
131
        @views.keys
132
    end
133

    
134
    def get_all_labels(group_name)
135
        labels = []
136
        if @views_config['labels_groups']
137
            if @views_config['labels_groups'][group_name]
138
                @views_config['labels_groups'][group_name].each{|l| labels.push(l)}
139
            end
140
            if @views_config['labels_groups']['default']
141
                @views_config['labels_groups']['default'].each{|l| labels.push(l)}
142
            end
143
        end
144
        return labels
145
    end
146

    
147
    def logo
148
        @views_config['logo']
149
    end
150
end