summaryrefslogtreecommitdiff
path: root/lib/chef/userable.rb
blob: 46b45d01cb36953ab4c793190107c684d7727f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#
# Author:: Snehal Dwivedi (sdwivedi@chef.io)
# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# common methods of User and UserV1
class Chef
  class BaseUser

    def initialize
      @name = ""
      @password = nil
      @admin = false
      @username = nil
      @display_name = nil
      @first_name = nil
      @middle_name = nil
      @last_name = nil
      @email = nil
      @public_key = nil
      @private_key = nil
      @create_key = nil
    end

    def chef_root_rest_v0
      @chef_root_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "0" })
    end

    def chef_root_rest_v1
      @chef_root_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "1" })
    end

    def name(arg = nil)
      set_or_return(:name, arg,
        regex: /^[a-z0-9\-_]+$/)
    end

    def admin(arg = nil)
      set_or_return(:admin,
        arg, kind_of: [TrueClass, FalseClass])
    end

    def username(arg = nil)
      set_or_return(:username, arg,
        regex: /^[a-z0-9\-_]+$/)
    end

    def display_name(arg = nil)
      set_or_return(:display_name,
        arg, kind_of: String)
    end

    def first_name(arg = nil)
      set_or_return(:first_name,
        arg, kind_of: String)
    end

    def middle_name(arg = nil)
      set_or_return(:middle_name,
        arg, kind_of: String)
    end

    def last_name(arg = nil)
      set_or_return(:last_name,
        arg, kind_of: String)
    end

    def email(arg = nil)
      set_or_return(:email,
        arg, kind_of: String)
    end

    def create_key(arg = nil)
      set_or_return(:create_key, arg,
        kind_of: [TrueClass, FalseClass])
    end

    def public_key(arg = nil)
      set_or_return(:public_key,
        arg, kind_of: String)
    end

    def private_key(arg = nil)
      set_or_return(:private_key,
        arg, kind_of: String)
    end

    def password(arg = nil)
      set_or_return(:password,
        arg, kind_of: String)
    end
  end
end