<commit_before>#!/usr/bin/env ruby

require 'digest/sha1'
require 'logger'

$USERMANAGER_URI="system/lite/userManager/"
$GROUP_URI="#{$USERMANAGER_URI}group.create.html"
$USER_URI="#{$USERMANAGER_URI}user.create.html"
$DEFAULT_PASSWORD="<PASSWORD>"

module SlingUsers

  class Principal

    attr_accessor :name

    def initialize(name)
      @name = name
    end


    # Get the public path for a user
    def public_path_for(sling)
      return home_path_for(sling) + "/public"
    end

    # Get the private path for a user
    def private_path_for(sling)
      return home_path_for(sling) + "/private"
    end

    def message_path_for(sling,messageid,mailbox)
      #return home_path_for(sling) + "/message/"+messageid[0,2]+"/"+messageid[2,2]+"/"+messageid[4,2]+"/"+messageid[6,2]+"/"+messageid
      return home_path_for(sling) + "/message/#{mailbox}/#{messageid}"
    end

  end


  class Group < Principal
    def to_s
      return "Group: #{@name}"
    end

    def update_properties(sling, props)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"), props)
    end

    def add_member(sling, principal, type)
      principal_path = "/#{$USERMANAGER_URI}#{type}/#{principal}"
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":member" => principal_path })
    end

    def add_members(sling, principals)
      principal_paths = principals.collect do |principal|
        if principal.index("g-") == 0
          type = "group"
        else
          type = "user"
        end
        "/#{$USERMANAGER_URI}#{type}/#{principal}"
      end
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":member" => principal_paths })
    end

    def add_manager(sling, principal)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":manager" => principal })
    end

    def add_viewer(sling, principal)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":viewer" => principal })
    end

    def details(sling)
      return sling.get_node_props(group_url)
    end

    def remove_member(sling, principal, type)
      principal_path = "/#{$USERMANAGER_URI}#{type}/#{principal}"
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":member@Delete" => principal_path })
    end

    def has_member(sling, principal)
      detail = self.details(sling)
      members = detail["members"]
      if (members == nil)
        return false
      end
      return members.include?(principal)
    end

    def remove_manager(sling, principal)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":manager@Delete" => principal })
    end

    def remove_viewer(sling, principal)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":viewer@Delete" => principal })
    end

    def remove_members(sling, principals)
      principal_paths = principals.collect do |principal|
        if principal.index("g-") == 0
          type = "group"
        else
          type = "user"
        end
        "/#{$USERMANAGER_URI}#{type}/#{principal}"
      end
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":member@Delete" => principal_paths })
    end

    def set_joinable(sling, joinable)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"), "sakai:joinable" => joinable)
    end

    def members(sling)
      props = sling.get_node_props(group_url)
      return props["members"]
    end

    # Get the home folder of a group.
    def home_path_for(sling)
      return "/~#{@name}"
    end

    def self.url_for(name)
      return "#{$USERMANAGER_URI}group/#{name}"
    end

    private
    def group_url
      return Group.url_for(@name)
    end
  end

  class Owner < Principal
	def initialize()
		super("owner")
	end
  end

  class User < Principal
    attr_accessor :password

    def initialize(username, password=$DEFAULT_PASSWORD)
      super(username)
      @password = password
    end

    def self.admin_user
      return User.new("admin", "admin")
    end

    def self.anonymous
      return AnonymousUser.new<commit_msg>KERN-1500 removed the concept of owner.<commit_after>#!/usr/bin/env ruby

require 'digest/sha1'
require 'logger'

$USERMANAGER_URI="system/lite/userManager/"
$GROUP_URI="#{$USERMANAGER_URI}group.create.html"
$USER_URI="#{$USERMANAGER_URI}user.create.html"
$DEFAULT_PASSWORD="<PASSWORD>"

module SlingUsers

  class Principal

    attr_accessor :name

    def initialize(name)
      @name = name
    end


    # Get the public path for a user
    def public_path_for(sling)
      return home_path_for(sling) + "/public"
    end

    # Get the private path for a user
    def private_path_for(sling)
      return home_path_for(sling) + "/private"
    end

    def message_path_for(sling,messageid,mailbox)
      #return home_path_for(sling) + "/message/"+messageid[0,2]+"/"+messageid[2,2]+"/"+messageid[4,2]+"/"+messageid[6,2]+"/"+messageid
      return home_path_for(sling) + "/message/#{mailbox}/#{messageid}"
    end

  end


  class Group < Principal
    def to_s
      return "Group: #{@name}"
    end

    def update_properties(sling, props)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"), props)
    end

    def add_member(sling, principal, type)
      principal_path = "/#{$USERMANAGER_URI}#{type}/#{principal}"
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":member" => principal_path })
    end

    def add_members(sling, principals)
      principal_paths = principals.collect do |principal|
        if principal.index("g-") == 0
          type = "group"
        else
          type = "user"
        end
        "/#{$USERMANAGER_URI}#{type}/#{principal}"
      end
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":member" => principal_paths })
    end

    def add_manager(sling, principal)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":manager" => principal })
    end

    def add_viewer(sling, principal)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":viewer" => principal })
    end

    def details(sling)
      return sling.get_node_props(group_url)
    end

    def remove_member(sling, principal, type)
      principal_path = "/#{$USERMANAGER_URI}#{type}/#{principal}"
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":member@Delete" => principal_path })
    end

    def has_member(sling, principal)
      detail = self.details(sling)
      members = detail["members"]
      if (members == nil)
        return false
      end
      return members.include?(principal)
    end

    def remove_manager(sling, principal)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":manager@Delete" => principal })
    end

    def remove_viewer(sling, principal)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":viewer@Delete" => principal })
    end

    def remove_members(sling, principals)
      principal_paths = principals.collect do |principal|
        if principal.index("g-") == 0
          type = "group"
        else
          type = "user"
        end
        "/#{$USERMANAGER_URI}#{type}/#{principal}"
      end
      return sling.execute_post(sling.url_for("#{group_url}.update.html"),
              { ":member@Delete" => principal_paths })
    end

    def set_joinable(sling, joinable)
      return sling.execute_post(sling.url_for("#{group_url}.update.html"), "sakai:joinable" => joinable)
    end

    def members(sling)
      props = sling.get_node_props(group_url)
      return props["members"]
    end

    # Get the home folder of a group.
    def home_path_for(sling)
      return "/~#{@name}"
    end

    def self.url_for(name)
      return "#{$USERMANAGER_URI}group/#{name}"
    end

    private
    def group_url
      return Group.url_for(@name)
    end
  end


  class User < Principal
    attr_accessor :password

    def initialize(username, password=$DEFAULT_PASSWORD)
      super(username)
      @password = password
    end

    def self.admin_user
      return User.new("admin", "admin")
    end

    def self.anonymous
      return AnonymousUser.new
{'fraction_non_alphanumeric': 0.13114561279698894, 'fraction_numerical': 0.0030581039755351682, 'mean_word_length': 2.8388261851015804, 'pattern_counts': {'":': 16, '<': 10, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4831946', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2752, 'n_words': 522}