kanal-plugins-user_system 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d8b808b891105793f573a9ad5761aafcfb63997d3d600bdd6ae157f7b6e326cf
4
+ data.tar.gz: 8feaa7ed933bb5ae1f4669cc12b762e8afdbb33e97ec855f72628515d059c2eb
5
+ SHA512:
6
+ metadata.gz: d92b41aeae204aa4a496b77042ac44ca3e4159d36aaa4bed5315b409a3aa3f914cfb6599f0dd7841e80a223c2b22c14ca86844adf437a5fd44b8be3044e40049
7
+ data.tar.gz: 9bc617ad85f52a023ca1bd8dc2a8dea3dd394c71bd07c517be9ea76f1d49be18a98271e9577d625f2a57196486bae045ba5353a405937fb6f100777f459ad3c2
data/.DS_Store ADDED
Binary file
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-11-14
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at idchlife@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in kanal-plugins-user_system.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "kanal"
13
+
14
+ group :development do
15
+ gem "rubocop", "~> 1.21"
16
+ gem "ruby-debug-ide"
17
+ gem "solargraph"
18
+ gem "yard"
19
+ end
20
+
21
+ group :test do
22
+ gem "kanal-plugins-active_record"
23
+ gem "simplecov", require: false
24
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,133 @@
1
+ GIT
2
+ remote: git@github.com:idchlife/kanal.git
3
+ revision: 5dbc20a3480337192d1193b0e569dba9d665d4c9
4
+ specs:
5
+ kanal (0.3.0)
6
+
7
+ PATH
8
+ remote: .
9
+ specs:
10
+ kanal-plugins-user_system (0.2.0)
11
+ activerecord
12
+ sqlite3
13
+
14
+ GEM
15
+ remote: https://rubygems.org/
16
+ specs:
17
+ activemodel (7.0.4.1)
18
+ activesupport (= 7.0.4.1)
19
+ activerecord (7.0.4.1)
20
+ activemodel (= 7.0.4.1)
21
+ activesupport (= 7.0.4.1)
22
+ activesupport (7.0.4.1)
23
+ concurrent-ruby (~> 1.0, >= 1.0.2)
24
+ i18n (>= 1.6, < 2)
25
+ minitest (>= 5.1)
26
+ tzinfo (~> 2.0)
27
+ ast (2.4.2)
28
+ backport (1.2.0)
29
+ benchmark (0.2.1)
30
+ concurrent-ruby (1.2.0)
31
+ diff-lcs (1.5.0)
32
+ docile (1.4.0)
33
+ e2mmap (0.1.0)
34
+ i18n (1.12.0)
35
+ concurrent-ruby (~> 1.0)
36
+ jaro_winkler (1.5.4)
37
+ json (2.6.3)
38
+ kanal-plugins-active_record (0.2.0)
39
+ activerecord
40
+ kanal
41
+ kramdown (2.4.0)
42
+ rexml
43
+ kramdown-parser-gfm (1.1.0)
44
+ kramdown (~> 2.0)
45
+ minitest (5.17.0)
46
+ nokogiri (1.14.0-x86_64-darwin)
47
+ racc (~> 1.4)
48
+ parallel (1.22.1)
49
+ parser (3.2.0.0)
50
+ ast (~> 2.4.1)
51
+ racc (1.6.2)
52
+ rainbow (3.1.1)
53
+ rake (13.0.6)
54
+ regexp_parser (2.6.2)
55
+ reverse_markdown (2.1.1)
56
+ nokogiri
57
+ rexml (3.2.5)
58
+ rspec (3.12.0)
59
+ rspec-core (~> 3.12.0)
60
+ rspec-expectations (~> 3.12.0)
61
+ rspec-mocks (~> 3.12.0)
62
+ rspec-core (3.12.0)
63
+ rspec-support (~> 3.12.0)
64
+ rspec-expectations (3.12.2)
65
+ diff-lcs (>= 1.2.0, < 2.0)
66
+ rspec-support (~> 3.12.0)
67
+ rspec-mocks (3.12.3)
68
+ diff-lcs (>= 1.2.0, < 2.0)
69
+ rspec-support (~> 3.12.0)
70
+ rspec-support (3.12.0)
71
+ rubocop (1.44.0)
72
+ json (~> 2.3)
73
+ parallel (~> 1.10)
74
+ parser (>= 3.2.0.0)
75
+ rainbow (>= 2.2.2, < 4.0)
76
+ regexp_parser (>= 1.8, < 3.0)
77
+ rexml (>= 3.2.5, < 4.0)
78
+ rubocop-ast (>= 1.24.1, < 2.0)
79
+ ruby-progressbar (~> 1.7)
80
+ unicode-display_width (>= 2.4.0, < 3.0)
81
+ rubocop-ast (1.24.1)
82
+ parser (>= 3.1.1.0)
83
+ ruby-debug-ide (0.7.3)
84
+ rake (>= 0.8.1)
85
+ ruby-progressbar (1.11.0)
86
+ simplecov (0.22.0)
87
+ docile (~> 1.1)
88
+ simplecov-html (~> 0.11)
89
+ simplecov_json_formatter (~> 0.1)
90
+ simplecov-html (0.12.3)
91
+ simplecov_json_formatter (0.1.4)
92
+ solargraph (0.48.0)
93
+ backport (~> 1.2)
94
+ benchmark
95
+ bundler (>= 1.17.2)
96
+ diff-lcs (~> 1.4)
97
+ e2mmap
98
+ jaro_winkler (~> 1.5)
99
+ kramdown (~> 2.3)
100
+ kramdown-parser-gfm (~> 1.1)
101
+ parser (~> 3.0)
102
+ reverse_markdown (>= 1.0.5, < 3)
103
+ rubocop (>= 0.52)
104
+ thor (~> 1.0)
105
+ tilt (~> 2.0)
106
+ yard (~> 0.9, >= 0.9.24)
107
+ sqlite3 (1.6.0-x86_64-darwin)
108
+ thor (1.2.1)
109
+ tilt (2.0.11)
110
+ tzinfo (2.0.5)
111
+ concurrent-ruby (~> 1.0)
112
+ unicode-display_width (2.4.2)
113
+ webrick (1.7.0)
114
+ yard (0.9.28)
115
+ webrick (~> 1.7.0)
116
+
117
+ PLATFORMS
118
+ x86_64-darwin-21
119
+
120
+ DEPENDENCIES
121
+ kanal!
122
+ kanal-plugins-active_record
123
+ kanal-plugins-user_system!
124
+ rake (~> 13.0)
125
+ rspec (~> 3.0)
126
+ rubocop (~> 1.21)
127
+ ruby-debug-ide
128
+ simplecov
129
+ solargraph
130
+ yard
131
+
132
+ BUNDLED WITH
133
+ 2.3.19
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 idchlife
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Kanal::Plugins::UserSystem
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kanal/plugins/user_system`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add kanal-plugins-user_system
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install kanal-plugins-user_system
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kanal-plugins-user_system. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/kanal-plugins-user_system/blob/main/CODE_OF_CONDUCT.md).
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the Kanal::Plugins::UserSystem project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/kanal-plugins-user_system/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/kanal/plugins/user_system/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "kanal-plugins-user_system"
7
+ spec.version = Kanal::Plugins::UserSystem::VERSION
8
+ spec.authors = ["idchlife"]
9
+ spec.email = ["idchlife@gmail.com"]
10
+
11
+ spec.summary = "User system plugin for kanal library, provides users, user properties, etc"
12
+ spec.description = "Library that provides user feature, with saving users and their properties"
13
+ spec.homepage = "https://github.com/idchlife/kanal-plugins-user_system"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.6"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/idchlife/kanal-plugins-user_system"
21
+ spec.metadata["changelog_uri"] = "https://github.com/idchlife/kanal-plugins-user_system"
22
+
23
+ spec.add_dependency "activerecord"
24
+ # spec.add_dependency "kanal"
25
+ spec.add_dependency "sqlite3"
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(__dir__) do
30
+ `git ls-files -z`.split("\x0").reject do |f|
31
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
32
+ end
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ # Uncomment to register a new dependency of your gem
39
+ # spec.add_dependency "example-gem", "~> 1.0"
40
+
41
+ # For more information and examples about making a new gem, check out our
42
+ # guide at: https://bundler.io/guides/creating_gem.html
43
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "active_record"
5
+
6
+ module Kanal
7
+ module Plugins
8
+ module UserSystem
9
+ module Helpers
10
+ # Helps using .value property with restoring/saving from
11
+ # json representation in .raw_value model field
12
+ module JsonValueConverterMixin
13
+ def value
14
+ JSON.parse(raw_value)
15
+ end
16
+
17
+ def value=(val)
18
+ self.raw_value = JSON.generate(val)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+
5
+ class CreateBaseModels < ActiveRecord::Migration[7.0]
6
+ def change
7
+ create_table :kanal_users do |t|
8
+ t.string :username, null: false, index: { unique: true, name: "kanal_unique_usernames" }
9
+ t.string :phone, null: true
10
+ t.string :email, null: true
11
+
12
+ t.timestamps
13
+ end
14
+
15
+ create_table :kanal_user_properties do |t|
16
+ t.string :name, null: false
17
+ t.string :raw_value, null: false
18
+ # t.string :type, null: false
19
+
20
+ t.timestamps
21
+ end
22
+
23
+ add_belongs_to :kanal_user_properties, :kanal_user, null: false, foreign_key: true
24
+
25
+ add_index :kanal_user_properties, [:name, :kanal_user_id], unique: true
26
+ end
27
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require_relative "./kanal_user_property"
5
+
6
+ module Kanal
7
+ module Plugins
8
+ module UserSystem
9
+ module Models
10
+ # Base user class for storing user properties
11
+ class KanalUser < ActiveRecord::Base
12
+ self.table_name = :kanal_users
13
+
14
+ STATE_PROPERTY = "_state"
15
+
16
+ has_many :kanal_user_properties
17
+
18
+ #
19
+ # Get all relatived properties for this user
20
+ #
21
+ # @return [Array<Kanal::Plugins::UserSystem::Models::KanalUserProperty>] <description>
22
+ #
23
+ def properties
24
+ kanal_user_properties
25
+ end
26
+
27
+ #
28
+ # Gets property by name if it exists for this user
29
+ #
30
+ # @param [String, Symbol] property_name <description>
31
+ #
32
+ # @return [Kanal::Plugins::UserSystem::Models::KanalUserProperty, nil] <description>
33
+ #
34
+ def get_property_by_name(property_name)
35
+ KanalUserProperty.find_by(kanal_user_id: id, name: property_name)
36
+ end
37
+
38
+ #
39
+ # Create KanalUserProperty and attach to this user
40
+ # Value of property can be any value that can be serialized
41
+ # and deserialized with JSON
42
+ #
43
+ # @param [String, Symbol] property_name <description>
44
+ # @param [Object] property_value <description>
45
+ #
46
+ # @return [KanalUserProperty] created or updates property
47
+ #
48
+ def create_or_update_property(property_name, property_value)
49
+ property = KanalUserProperty.find_by(kanal_user_id: id, name: property_name)
50
+
51
+ property ||= KanalUserProperty.create(
52
+ kanal_user_id: id,
53
+ name: property_name,
54
+ raw_value: JSON.generate(property_value)
55
+ )
56
+
57
+ property.value = property_value
58
+
59
+ property.save
60
+ end
61
+
62
+ #
63
+ # Get current users state
64
+ #
65
+ # @return [String] <description>
66
+ #
67
+ def state
68
+ prop = get_property_by_name STATE_PROPERTY
69
+
70
+ return nil if prop.nil?
71
+
72
+ prop.value
73
+ end
74
+
75
+ #
76
+ # Set state for user
77
+ #
78
+ # @param [String] value <description>
79
+ #
80
+ # @return [void] <description>
81
+ #
82
+ def state=(state_value)
83
+ create_or_update_property STATE_PROPERTY, state_value
84
+ end
85
+
86
+ #
87
+ # Find all users with needed state. It's possible to limit and offset
88
+ #
89
+ # @param [String] state_value <description>
90
+ # @param [Integer] limit <description>
91
+ # @param [Integer] offset <description>
92
+ #
93
+ # @return [Array<Kanal::Plugins::UserSystem::Models::KanalUser>] <description>
94
+ #
95
+ def self.find_all_by_state(state_value, limit: nil, offset: nil)
96
+ find_all_by_property(
97
+ property_name: STATE_PROPERTY,
98
+ property_value: state_value,
99
+ limit: limit,
100
+ offset: offset
101
+ )
102
+ end
103
+
104
+ #
105
+ # Find all users that have the specified property.
106
+ # If you also specify the property_value, you will get
107
+ # users that also has the same value.
108
+ # You can also optionally use limit and offset for pagination or simply getting
109
+ # the preferred size of result
110
+ #
111
+ # @example
112
+ # users_with_big_nose = KanalUser.find_all_by_property(property_name: "nose", property_value: "big")
113
+ #
114
+ # @param [String, Symbol] property_name <description>
115
+ # @param [Object] property_value <description>
116
+ # @param [Integer] limit limit result
117
+ # @param [Integer] offset offset for result
118
+ #
119
+ # @return [Array<Kanal::Plugins::UserSystem::Models::KanalUser>] <description>
120
+ #
121
+ def self.find_all_by_property(property_name:, property_value: nil, limit: nil, offset: nil)
122
+ if !property_value.nil?
123
+ users = KanalUser.includes(:kanal_user_properties).where(
124
+ "kanal_user_properties.name" => property_name.to_s,
125
+ "kanal_user_properties.raw_value" => JSON.generate(property_value)
126
+ )
127
+ else
128
+ users = KanalUser.includes(:kanal_user_properties).where(
129
+ "kanal_user_properties.name" => property_name.to_s
130
+ )
131
+ end
132
+
133
+ users.limit(limit) unless limit.nil?
134
+ users.offset(offset) unless offset.nil?
135
+
136
+ users
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require_relative "../helpers/json_value_converter_mixin"
5
+
6
+ module Kanal
7
+ module Plugins
8
+ module UserSystem
9
+ module Models
10
+ # Base user class for storing user properties
11
+ class KanalUserProperty < ActiveRecord::Base
12
+ include Helpers::JsonValueConverterMixin
13
+
14
+ self.table_name = :kanal_user_properties
15
+
16
+ belongs_to :kanal_users
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "kanal/core/plugins/plugin"
4
+ require_relative "./models/kanal_user"
5
+
6
+ module Kanal
7
+ module Plugins
8
+ module UserSystem
9
+ # User system plugin serves as variant of user authentication,
10
+ # registration and removal system
11
+ class UserSystemPlugin < Kanal::Core::Plugins::Plugin
12
+ include Models
13
+
14
+ def name
15
+ :user_system
16
+ end
17
+
18
+ #
19
+ # @param [Kanal::Core::Core] core <description>
20
+ #
21
+ # @return [void] <description>
22
+ #
23
+ def setup(core)
24
+ unless core.plugin_registered? :active_record
25
+ raise "Cannot setup UserSystem without :active_record plugin installed!"
26
+ end
27
+
28
+ active_record_plugin = core.get_plugin :active_record
29
+
30
+ active_record_plugin.add_migrations_directory File.join(__dir__, "migrations")
31
+
32
+ setup_user_storage core
33
+ setup_user_state core
34
+ end
35
+
36
+ def setup_user_state(core)
37
+ user_exists = ->(i) { i.user.is_a? KanalUser }
38
+
39
+ core.add_condition_pack :user_state do
40
+ add_condition :is do
41
+ with_argument
42
+
43
+ met? do |input, core, argument|
44
+ return false unless user_exists.call input
45
+
46
+ check_for_state = argument
47
+
48
+ check_for_state = check_for_state.to_s if check_for_state.is_a? Symbol
49
+
50
+ current_state = input.user.state
51
+
52
+ current_state == check_for_state
53
+ end
54
+ end
55
+
56
+ add_condition :not_set do
57
+ met? do |input, core, argument|
58
+ return false unless user_exists.call input
59
+
60
+ input.user.state.nil?
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ def setup_user_storage(core)
67
+ core.register_input_parameter :user
68
+
69
+ user_exists = ->(i) { i.user.is_a? KanalUser }
70
+
71
+ core.add_condition_pack :user do
72
+ add_condition :exists do
73
+ met? do |input, _, _|
74
+ user_exists.call input
75
+ end
76
+ end
77
+
78
+ add_condition :has_property_value do
79
+ with_argument
80
+
81
+ met? do |input, c, argument|
82
+ return false unless user_exists.call input
83
+
84
+ property_name = argument[0]
85
+ value = argument[1]
86
+
87
+ property = input.user.get_property_by_name property_name
88
+
89
+ return false if property.nil?
90
+
91
+ property_value = property.value
92
+
93
+ return property_value == value
94
+ end
95
+ end
96
+
97
+ add_condition :phone_one_of do
98
+ with_argument
99
+
100
+ met? do |input, _, argument|
101
+ return false unless user_exists.call input
102
+
103
+ argument.include? input.user.phone
104
+ end
105
+ end
106
+
107
+ add_condition :email_one_of do
108
+ with_argument
109
+
110
+ met? do |input, _, argument|
111
+ return false unless user_exists.call input
112
+
113
+ argument.include? input.user.email
114
+ end
115
+ end
116
+
117
+ add_condition :username_one_of do
118
+ with_argument
119
+
120
+ met? do |input, _, argument|
121
+ return false unless user_exists.call input
122
+
123
+ return argument.include? input.user.username
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanal
4
+ module Plugins
5
+ module UserSystem
6
+ VERSION = "0.2.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "user_system/version"
4
+
5
+ module Kanal
6
+ module Plugins
7
+ module UserSystem
8
+ class Error < StandardError; end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Kanal
2
+ module Plugins
3
+ module UserSystem
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kanal-plugins-user_system
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - idchlife
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Library that provides user feature, with saving users and their properties
42
+ email:
43
+ - idchlife@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".DS_Store"
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - ".ruby-version"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - kanal-plugins-user_system.gemspec
60
+ - lib/kanal/plugins/user_system.rb
61
+ - lib/kanal/plugins/user_system/database/database_connection.rb
62
+ - lib/kanal/plugins/user_system/helpers/json_value_converter_mixin.rb
63
+ - lib/kanal/plugins/user_system/migrations/20221124150000_create_base_models.rb
64
+ - lib/kanal/plugins/user_system/models/kanal_user.rb
65
+ - lib/kanal/plugins/user_system/models/kanal_user_property.rb
66
+ - lib/kanal/plugins/user_system/user_system_plugin.rb
67
+ - lib/kanal/plugins/user_system/version.rb
68
+ - sig/kanal/plugins/user_system_plugin.rbs
69
+ homepage: https://github.com/idchlife/kanal-plugins-user_system
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ allowed_push_host: https://rubygems.org
74
+ homepage_uri: https://github.com/idchlife/kanal-plugins-user_system
75
+ source_code_uri: https://github.com/idchlife/kanal-plugins-user_system
76
+ changelog_uri: https://github.com/idchlife/kanal-plugins-user_system
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 2.7.6
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.1.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: User system plugin for kanal library, provides users, user properties, etc
96
+ test_files: []