123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- <style>
- .editButton{
- color: #83b3d2;
- cursor: pointer;
- float: right;
- }
- </style>
- <!-- <h3>Enable Samba Service</h3>
- <p>Change current systemctl state of smbd (start / stop).</p>
- <div class="ui toggle checkbox">
- <input type="checkbox" id="enablesmbd">
- <label>Enable smbd (Samba Sharing Service)</label>
- </div>
- -->
- <div class="ui divider"></div>
- <h3><i class="ui green share alternate icon"></i> Samba Share Lists</h3>
- <p>A list of SMB shares currently written into smb.conf</p>
- <div style="width: 100%; overflow-y: auto;">
- <div id="sharelist">
- </div>
- <br>
- </div>
- <!-- Create new Samba Share -->
- <h4><i class="ui green circle add icon"></i> Add Samba Share</h4>
- <p>Create a new SMB share folder from local disk</p>
- <form class="ui form" id="shareForm">
- <div class="field">
- <label for="shareName">Share Name</label>
- <input type="text" id="shareName" placeholder="Share Name">
- </div>
- <div class="field">
- <label for="sharePath">Share Path</label>
- <input type="text" id="sharePath" placeholder="/home/user/myshare">
- <small><i class="ui yellow exclamation triangle icon"></i> The folder path must be an absolute full path exists on your local disk. e.g. /home/user/myshare/ </small>
- </div>
- <div class="field">
- <label for="validUsers">Valid Users</label>
- <select multiple="" class="ui search dropdown" id="validUsers">
- </select>
- <button onclick="event.preventDefault(); initSambaUserList();" class="ui mini basic button" style="margin-top: 0.4em;"><i class="ui green refresh icon"></i> Refresh User List</button>
- </div>
- <div class="field">
- <div class="ui checkbox">
- <input type="checkbox" id="readOnly">
- <label for="readOnly">Read Only<br>
- <small>Set all files in this share to READ ONLY mode</small></label>
- </div>
- </div>
- <div class="field">
- <div class="ui checkbox">
- <input type="checkbox" id="browseable" checked>
- <label for="browseable">Browseable <br>
- <small>Make this share discoverable</small></label>
- </div>
- </div>
- <div class="field">
- <div class="ui checkbox">
- <input type="checkbox" id="allowGuest">
- <label for="allowGuest">Allow Guest<br>
- <small>Enable guest account on this share</small></label>
- </div>
- </div>
- <button type="button" class="ui small basic button" onclick="newSambaShare(); event.preventDefault();"><i class="ui green circle add icon"></i> Create Share</button>
- </form>
- <!-- Create new Samba user -->
- <div class="ui divider"></div>
- <h3><i class="ui green user circle icon"></i> Samba Users List</h3>
- <p>Current list of users registered in Samba database</p>
- <div id="userTableContainer"></div>
- <h4><i class="ui green user plus icon"></i> Add Samba User</h4>
- <p>Create Samba user for sharing<br>
- <small>Samba user is not ArozOS user. Creating a Samba user will also create a unix user with login function disabled</small></p>
- <div class="ui container">
- <form class="ui form" id="userForm">
- <div class="field">
- <label for="username">Username</label>
- <input type="text" id="smbuser_username" placeholder="Enter username" required>
- </div>
- <div class="field">
- <label for="password">Password</label>
- <input type="password" id="smbuser_password" placeholder="Enter password" required>
- </div>
- <div class="field">
- <label for="confirmPassword">Confirm Password</label>
- <input type="password" id="smbuser_confirmPassword" placeholder="Confirm password" required>
- </div>
- <button type="button" class="ui basic button" onclick="createNewSambaUser()"><i class="ui checkmark green icon"></i> Confirm</button>
- </form>
- </div>
- <script>
- $("#validUsers").dropdown();
- $("#shareForm").find("checkbox").checkbox();
- //Load all the users in the samba database
- function initSambaUserList(){
- $.get("../../system/storage/samba/listUsers", function(data){
- if (data.error == undefined){
- $("#validUsers").html("");
- renderUserTable(data);
- if (data.length == 0){
- return;
- }
- data.forEach(function(userinfo){
- $("#validUsers").append(`<option value="${userinfo.UnixUsername}">${userinfo.UnixUsername}</option>`);
- })
- }
- });
- }
- initSambaUserList();
- //List the current shares in smb.conf
- function initShareListTable(){
- $.get("../../system/storage/samba/list", function(data){
- if (data.error){
- msgbox(data.error, false);
- }else{
- data.sort(function(a, b) {
- if (a.Name < b.Name) return -1;
- if (a.Name > b.Name) return 1;
- return 0;
- });
- generateTable(data);
- }
- });
- }
- initShareListTable();
- //Load current smbd state
- function initSmbdState(){
- $.get("../../system/storage/samba/status", function(data){
- if (data.error != undefined){
- //Error when loading smbd status
- msgbox(data.error, false);
- $("#enablesmbd").parent().addClass('disabled');
- }else{
- if (data == true){
- $("#enablesmbd").parent().checkbox("set checked");
- }else{
- $("#enablesmbd").parent().checkbox("set unchecked");
- }
- //Bind checkbox event
- $("#enablesmbd").on("change", function(data){
- let isChecked = $("#enablesmbd")[0].checked;
- $.ajax({
- url: "../../system/storage/samba/status",
- data: {
- set: isChecked?"enable":"disable"
- },
- method: "POST",
- success: function(data){
- if (data.error != undefined){
- msgbox(data.error, false);
- $("#enablesmbd").off("change");
- initSmbdState();
- }else{
- msgbox(`SMB Sharing ${isChecked?"enabled":"disabled"}`);
- }
- }
- })
- })
- }
- })
- }
- initSmbdState();
- function generateTable(data) {
- // Start the table
- let table = `
- <table class="ui basic celled unstackable table">
- <thead>
- <tr>
- <th style="min-width: 100px;"><i class="ui yellow folder icon"></i> Name</th>
- <th style="min-width: 100px;"><i class="ui grey hdd icon"></i> Path</th>
- <th style="min-width: 100px;">Valid Users</th>
- <th>Read Only</th>
- <th>Browseable</th>
- <th>Guest Ok</th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- `;
- // Populate the table rows
- data.forEach(item => {
- let userListHex = encodeURIComponent(JSON.stringify([]));
- if (item.ValidUsers != null){
- userListHex = encodeURIComponent(JSON.stringify(item.ValidUsers));
- }
-
- table += `
- <tr>
- <td>${item.Name}</td>
- <td>${item.Path} <span class="editButton" onclick="editSharePath('${item.Name}', '${item.Path}');"><i class="edit icon"></i></span></td>
- <td>${(item.ValidUsers!=null)?item.ValidUsers.join(", "):'[unix user]'} ${(item.ValidUsers!=null)?`<span class="shareuserEditBtn editButton" onclick="editSambaUser(this, '${item.Name}');" users="${userListHex}"><i class="edit icon"></i></span>`:""}</td>
- <td>${item.ReadOnly?'<i class="ui green check icon"></i>':'<i class="ui red times icon"></i>'}</td>
- <td>${item.Browseable?'<i class="ui green check icon"></i>':'<i class="ui red times icon"></i>'}</td>
- <td>${item.GuestOk?'<i class="ui green check icon"></i>':'<i class="ui red times icon"></i>'}</td>
- <td><button title="Remove Share" class="ui basic circular tiny red circular icon button" onclick="deleteSMBShare('${item.Name}');"><i class="ui trash icon"></i></button></td>
- </tr>
- `;
- });
- // Close the table
- table += `
- </tbody>
- </table>
- `;
- if (data.length == 0){
- table = `<div class="ui basic message"><i class="ui green check circle icon"></i> No Samba share folder found in smb.conf</div>`
- }
- // Insert the table into the div
- $("#sharelist").html(table);
- }
- //Create a new samba share
- function newSambaShare(){
- let shareName = $("#shareName").val().trim();
- let sharePath = $("#sharePath").val().trim();
- let allowedUsers = $("#validUsers").dropdown("get value");
- let isReadOnly = $("#readOnly")[0].checked;
- let isBrowseable = $("#browseable")[0].checked;
- let allowGuest = $("#allowGuest")[0].checked;
- if (shareName == ""){
- $("#shareName").parent().addClass("error");
- msgbox("Share name cannot be empty", false);
- return;
- }else{
- $("#shareName").parent().removeClass("error");
- }
- if (sharePath == ""){
- $("#sharePath").parent().addClass("error");
- msgbox("Share path cannot be empty", false);
- return;
- }else{
- $("#sharePath").parent().removeClass("error");
- }
- if (allowedUsers.length == 0){
- msgbox("At least one user is required to create share");
- return;
- }
- $.ajax({
- url: "../../system/storage/samba/add",
- method: "POST",
- data: {
- "name":shareName,
- "path": sharePath,
- "users": JSON.stringify(allowedUsers),
- "readonly":isReadOnly,
- "browseable": isBrowseable,
- "guestok":allowGuest
- },
- success: function(data){
- if (data.error != undefined){
- msgbox(data.error, false, 6000);
- }else{
- msgbox("New Samba share created");
- }
- initShareListTable();
- }
- })
- }
- //Delete the given smb share name
- function deleteSMBShare(smbShareName){
- if (confirm("Confirm remove share " + smbShareName + " ?")){
- $.ajax({
- url: "../../system/storage/samba/remove",
- method: "POST",
- data:{
- "name": smbShareName,
- },
- success: function(data){
- if (data.error != undefined){
- msgbox(data.error, false);
- }else{
- msgbox("Samba share removed");
- }
- initShareListTable();
- //Reload user smb as well if user is admin and have their shares removed
- if (typeof(initSMBActivationStatusOnThisUser) != 'undefined'){
- initSMBActivationStatusOnThisUser();
- }
- }
- })
- }
- }
- //Render current list of users in samba
- function renderUserTable(data) {
- // Start the table
- let table = `
- <table class="ui celled basic small table">
- <thead>
- <tr>
- <th>Unix Username</th>
- <th>Domain</th>
- <th>ArozOS User</th>
- <th>Remove</th>
- </tr>
- </thead>
- <tbody>
- `;
- // Populate the table rows
- data.forEach(item => {
- table += `
- <tr>
- <td><img class="ui avatar image" src="/system/users/profilepic?user=${item.UnixUsername}"> ${item.UnixUsername}</td>
- <td>${item.Domain}</td>
- <td>${item.IsArozOSUser?"<i class='ui green check icon'></i>":"<i class='ui red times icon'></i>"}</td>
- <td><button class="ui basic small red button" onclick="removeSambaUser('${item.UnixUsername}');"><i class="ui red trash icon"></i> Remove Samba User</button></td>
- </tr>
- `;
- });
- if (data.length == 0){
- table += `<tr><td colspan="3"><i class="ui green circle check icon"></i> No registered users in Samba database</td></tr>`;
- }
- // Close the table
- table += `
- </tbody>
- </table>
- `;
- // Insert the table into the div
- $('#userTableContainer').html(table);
- }
- //Create a new samba user
- function createNewSambaUser(){
- // Get values from the form
- const username = $('#smbuser_username').val();
- const password = $('#smbuser_password').val();
- const confirmPassword = $('#smbuser_confirmPassword').val();
- // Check if passwords match
- if (password !== confirmPassword) {
- msgbox("Confirm password does not match!", false);
- }
- $.ajax({
- url: "/system/storage/samba/addUser",
- method: "POST",
- data: {
- "username": username,
- "password": password
- },
- success: function(data){
- if (data.error != undefined){
- msgbox(data.error, false, 5000);
- }else{
- msgbox("New Samba user created");
- $("#smbuser_username").val("");
- $("#smbuser_password").val("");
- $("#smbuser_confirmPassword").val("");
- }
- //Update the samba user list in share
- initSambaUserList();
- }
- })
- }
- //Remove a samba user given the username
- function removeSambaUser(targetUsername){
- if (confirm("Confirm remove samba user \"" + targetUsername + "\" ?")){
- $.ajax({
- url: "/system/storage/samba/delUser",
- method: "POST",
- data: {
- "username": targetUsername
- },
- success: function(data){
- if (data.error != undefined){
- msgbox(data.error, false, 5000);
- }else{
- msgbox("Samba user removed");
- }
- //Update the samba user list in share
- initSambaUserList();
- }
- });
- }
- }
- //Inline edit for samba users
- function editSambaUser(targetDom, shareName){
- let originalUserList = JSON.parse(decodeURIComponent($(targetDom).attr("users")));
- let fieldElement = $(targetDom).parent();
- $(fieldElement).html(`<i class="loading spinner icon"></i> Loading User List`);
- //Overwrite the DOM element with multi-selection dropdown and save button
- $.get("../../system/storage/samba/listUsers", function(data){
- if (data.error == undefined){
- $(".shareuserEditBtn").remove();
- //Append user selector with default value selected
- $(fieldElement).html(`<select multiple="" class="ui search dropdown" id="editValidUserList"></select>`);
- data.forEach(function(userinfo){
- $("#editValidUserList").append(`<option value="${userinfo.UnixUsername}">${userinfo.UnixUsername}</option>`);
- });
- $("#editValidUserList").dropdown();
- $("#editValidUserList").dropdown("set selected", originalUserList);
- //Append save and cancel button
- $(fieldElement).append(`
- <div style="margin-top: 0.6em;">
- <button class="ui small basic button" onclick="saveSambaUserEdit('${shareName}');"><i class="ui green save icon"></i> Save</button>
- <button class="ui small basic button" onclick="initShareListTable();"><i class="ui grey remove icon"></i> Cancel</button>
- </div>
- `);
- }else{
- msgbox("Failed to connect to smbd service", false)
- }
- });
- }
- function saveSambaUserEdit(shareName){
- //Read selection from #editValidUserList
- let allowedUsers = $("#editValidUserList").dropdown("get value");
- if (allowedUsers.length == 0){
- msgbox("At least one user is required per share", false);
- return
- }
- $.ajax({
- url: "/system/storage/samba/updateShareUsers",
- method: "POST",
- data: {
- "name": shareName,
- "users": JSON.stringify(allowedUsers)
- },
- success: function(data){
- if (data.error != undefined){
- msgbox(data.error, false);
- }else{
- msgbox("Accessible users updated");
- }
- //Clear share table
- initShareListTable();
- }
- });
- }
- function editSharePath(shareName, originalSharePath){
- let newpath = prompt("New Share Path", originalSharePath);
- if (newpath != null && newpath != "") {
- $.ajax({
- url: "/system/storage/samba/editPath",
- method: "POST",
- data: {
- "name": shareName,
- "path": newpath
- },
- success: function(data){
- if (data.error != undefined){
- msgbox(data.error, false);
- }else{
- msgbox("Share path updated");
- }
- //Clear share table
- initShareListTable();
- }
- });
- }
- }
- </script>
|