lita-ldap 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3167e6fec632522988a1c129cc05aa2939e47ee3
4
- data.tar.gz: 3fbf2d94042554e7eaa663c3513fc8ef8f01fb0a
3
+ metadata.gz: dd7f06cd70351f1cb694ef2c370a21871be3d116
4
+ data.tar.gz: c8bbe175bd45feff62ec4538e5d53afc13fd6729
5
5
  SHA512:
6
- metadata.gz: f08e28135ac41a108d49a642b33b9626c4a9719f7348c2d71e483f046262c317265735e0d04f01f9d10b79f9376b5268366f0397d2c20d307fd4021ea47b422b
7
- data.tar.gz: d3a45ddb8ff0d6a46ffa6b8c55224d4c5b0e1d34b4e62de2b89efad9762f452b34d41bb4e9a7460df60c4f5ecdd3559fb9911b843bd0b7a4b51787d3437d863c
6
+ metadata.gz: 9fe2c4a6547e01d8f91688b9c8b67e38bc10c6e8d1d6e94538b71f29eb0311c70a506beb4b4ab3ec3456ff84698e947ef1ce6985c6e03bd4de41aa0b66ff21c1
7
+ data.tar.gz: 82b6043191d6158b0ea203cab51595195e6cecda36a9a47f3d1d330e74be8811f36e00275836ac451b03defe918bd881537786f17406e29bf85bac9b89bd911b
data/README.md CHANGED
@@ -29,3 +29,5 @@ end
29
29
 
30
30
  * lita search user nexus
31
31
  * lita search group nx-admin
32
+ * lita search with filter 'cn=gerrit'
33
+ * lita show dn 'cn=gerrit,ou=people,dc=ldap,dc=example,dc=com'
@@ -17,5 +17,51 @@ module LitaLDAPHelper
17
17
  raise "Cannot connect to ldap server"
18
18
  end
19
19
  end
20
- end
21
- end
20
+
21
+ def valid_filter?(filter_str)
22
+ begin
23
+ filter=Net::LDAP::Filter.construct(filter_str)
24
+ true
25
+ rescue Exception => e
26
+ false
27
+ end
28
+ end
29
+
30
+ def search_with_filter(filter_str)
31
+ tree_base = "#{config.base_dn}"
32
+ puts "search base_dn : #{tree_base}"
33
+ filter=Net::LDAP::Filter.construct(filter_str)
34
+ return_attributes = config.default_attributes || '*'
35
+ return_attributes = return_attributes.split(',')
36
+ entries = client.search(:base => tree_base, :filter => filter, :attributes =>return_attributes , :return_result => true)
37
+ results = []
38
+ unless entries.nil?
39
+ entries.each do |entry|
40
+ results << entry.to_ldif
41
+ results << '*********'
42
+ end
43
+ end
44
+ #puts results
45
+ results
46
+ end
47
+
48
+ def get_entry_by_dn(dn)
49
+ dn_spec = dn.split(':',2)
50
+ dn_str = dn_spec[0]
51
+ if dn_spec.length>1
52
+ dn_str = dn_spec[1]
53
+ end
54
+ dn_str = dn_str.strip
55
+ entries = client.search(:base => dn_str, :filter => nil, :attributes =>['*'] , :return_result => true)
56
+ results = []
57
+ unless entries.nil?
58
+ entries.each do |entry|
59
+ results << entry.to_ldif
60
+ results << '*********'
61
+ end
62
+ end
63
+ #puts results
64
+ results
65
+ end
66
+ end#module misc
67
+ end#module LitaLDAPHelper
@@ -55,6 +55,36 @@ module Lita
55
55
  }
56
56
  )
57
57
 
58
+ #https://dzone.com/articles/matching-quoted-strings-ruby
59
+ #matching quoted string
60
+ route(
61
+ /^ldap\s+check\s+filter\s+(["'])([^\1]+)(\1)$/,
62
+ :cmd_check_filter,
63
+ command: true,
64
+ help: {
65
+ t('help.cmd_check_filter_key') => t('help.cmd_check_filter_value')
66
+ }
67
+ )
68
+
69
+ route(
70
+ /^ldap\s+search\s+with\s+filter\s+(["'])([^\1]+)(\1)$/,
71
+ :cmd_search_with_filter,
72
+ command: true,
73
+ help: {
74
+ t('help.cmd_search_with_filter_key') => t('help.cmd_search_with_filter_value')
75
+ }
76
+ )
77
+
78
+ route(
79
+ /^ldap\s+show\s+dn\s+(["'])([^\1]+)(\1)$/,
80
+ :cmd_search_with_dn,
81
+ command: true,
82
+ help: {
83
+ t('help.cmd_search_with_dn_key') => t('help.cmd_search_with_dn_value')
84
+ }
85
+ )
86
+
87
+
58
88
  def cmd_search_user(response)
59
89
  search_string = response.matches[0][0]
60
90
  #logger.info "searching user with #{search_string}"
@@ -69,6 +99,46 @@ module Lita
69
99
  response.reply results
70
100
  end
71
101
 
102
+ def cmd_check_filter(response)
103
+ filter_string = response.matches[0][1]
104
+ #puts "filter_string: #{filter_string}"
105
+ if ! filter_string.nil? && filter_string.strip.length>0
106
+ is_valid = valid_filter?(filter_string)
107
+ if is_valid
108
+ response.reply "Filter is valid"
109
+ else
110
+ response.reply "Filter in not valid"
111
+ end
112
+ else
113
+ response.reply "Filter string is empty"
114
+ end
115
+ end
116
+
117
+ def cmd_search_with_filter(response)
118
+ filter_string = response.matches[0][1]
119
+ if ! filter_string.nil? && filter_string.strip.length>0
120
+ is_valid = valid_filter?(filter_string)
121
+ if is_valid
122
+ results = search_with_filter(filter_string)
123
+ response.reply results
124
+ else
125
+ response.reply "Filter in not valid"
126
+ end
127
+ else
128
+ response.reply "Filter string is empty"
129
+ end
130
+ end
131
+
132
+ def cmd_search_with_dn(response)
133
+ dn = response.matches[0][1]
134
+ if ! dn.nil? && dn.strip.length>0
135
+ results = get_entry_by_dn(dn)
136
+ response.reply results
137
+ else
138
+ response.reply "Invalid dn"
139
+ end
140
+ end
141
+
72
142
  Lita.register_handler(self)
73
143
  end #class
74
144
  end # module
data/lita-ldap.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-ldap"
3
- spec.version = "0.1.0"
3
+ spec.version = "0.1.1"
4
4
  spec.authors = ["Wang, Dawei"]
5
5
  spec.email = ["daweiwang.gatekeeper@gmail.com"]
6
6
  spec.description = "Lita LDAP operations like search user and group."
data/locales/en.yml CHANGED
@@ -6,3 +6,9 @@ en:
6
6
  cmd_search_user_value: 'Search user with search string'
7
7
  cmd_search_group_key: 'ldap search user SEARCH_STRING'
8
8
  cmd_search_group_value: 'Search user with search string'
9
+ cmd_check_filter_key: 'ldap check filter "FILTER_STRING"'
10
+ cmd_check_filter_value: 'Check ldap filter syntax'
11
+ cmd_search_with_filter_key: 'ldap search with filter "FILTER_STRING"'
12
+ cmd_search_with_filter_value: 'Search entry with filter "FILTER_STRING"'
13
+ cmd_search_with_dn_key: 'ldap show dn "DN_STRING"'
14
+ cmd_search_with_dn_value: 'Show entry with dn "DN_STRING"'
@@ -55,6 +55,8 @@ describe Lita::Handlers::Ldap, lita_handler: true do
55
55
  it do
56
56
  is_expected.to route_command('ldap search user nexus').to(:cmd_search_user)
57
57
  is_expected.to route_command('ldap search group nx-admin').to(:cmd_search_group)
58
+ is_expected.to route_command('ldap check filter "cn=admin"').to(:cmd_check_filter)
59
+ is_expected.to route_command('ldap search with filter "cn=nexus"').to(:cmd_search_with_filter)
58
60
  end
59
61
 
60
62
  describe '#search user' do
@@ -70,4 +72,26 @@ describe Lita::Handlers::Ldap, lita_handler: true do
70
72
  puts replies
71
73
  end
72
74
  end
75
+
76
+ describe '#check filter' do
77
+ it 'validate filter syntax' do
78
+ send_command("ldap check filter '(|(objectclass=user)(objectclass=person)(objectclass=inetOrgPerson)(objectclass=organizationalPerson))'")
79
+ puts replies
80
+ end
81
+ end
82
+
83
+ describe '#search with filter' do
84
+ it 'search with filter' do
85
+ send_command("ldap search with filter 'cn=gerrit'")
86
+ puts replies
87
+ end
88
+ end
89
+
90
+ describe '#show dn' do
91
+ it 'show entry with specified dn' do
92
+ send_command("ldap show dn 'dn: cn=nx-admin,ou=groups,dc=ldap,dc=example,dc=com'")
93
+ puts "ldap show dn"
94
+ puts replies
95
+ end
96
+ end
73
97
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-ldap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wang, Dawei
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-06 00:00:00.000000000 Z
11
+ date: 2016-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack